[issue43946] unpickling a subclass of list fails when it implements its own extend method

Richard Levasseur report at bugs.python.org
Tue Apr 27 00:00:15 EDT 2021


Richard Levasseur <richardlev at gmail.com> added the comment:

Here's a self-contained repro:


```
import pickle

class MyList(list):
  def __init__(self, required, values):
    self.required = required
    super().__init__(values)

  def __getstate__(self):
    return self.required

  def __setstate__(self, state):
    self.required = state

  def extend(self, values):
    assert self.required
    super().extend(values)

mylist = MyList('foo', [1, 2])
pickled = pickle.dumps(mylist)
unpickled = pickle.loads(pickled)

print(mylist)

```

The above will raise an AttributeError when self.required is accessed in extend(). 

Oddly, defining a `__reduce__()` function that simply calls and returns `super().__reduce__()` seems to restore the previous behavior and things work again.

----------
nosy: +richardlev

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43946>
_______________________________________


More information about the Python-bugs-list mailing list