Message409867
This isn't really an issue with exceptions, it's just that __reduce__ goes out of sync with the object's constructor signature. Here's a simple example of the same:
class Base:
def __init__(self, msg):
self.msg = msg
def __reduce__(self):
return type(self), (self.msg,)
class Derived(Base):
def __init__(self, a, b):
super().__init__(f'{a}|{b}')
x = Derived('a', 'b')
assert x.msg == 'a|b'
y = pickle.dumps(x, -1)
z = pickle.loads(y)
-------------------------------------------
Output:
Traceback (most recent call last):
File "/Users/iritkatriel/src/cpython/zz.py", line 22, in <module>
z = pickle.loads(y)
^^^^^^^^^^^^^^^
TypeError: Derived.__init__() missing 1 required positional argument: 'b'
If I define __reduce__ on Derived to return an arg tuple of the right length for its __init__, it works:
class Derived(Base):
def __init__(self, a, b):
super().__init__(f'{a}|{b}')
def __reduce__(self):
return type(self), tuple(self.msg.split('|'))
But note that this is not something we could make the base class do, it has no way of finding out what the semantics of the derived class constructor args are. |
|
Date |
User |
Action |
Args |
2022-01-06 15:54:13 | iritkatriel | set | recipients:
+ iritkatriel, georg.brandl, jaraco, zseil, alexandre.vassalotti, orivej, sbt, cryvate, slallum, Kirill Matsaberydze, benoit-pierre, 4-launchpad-kalvdans-no-ip-org |
2022-01-06 15:54:13 | iritkatriel | set | messageid: <1641484453.89.0.640915707568.issue32696@roundup.psfhosted.org> |
2022-01-06 15:54:13 | iritkatriel | link | issue32696 messages |
2022-01-06 15:54:13 | iritkatriel | create | |
|