cpython icon indicating copy to clipboard operation
cpython copied to clipboard

dataclasses: pickling frozen dataclasses which derive from classes with slots does not work.

Open avboag opened this issue 2 years ago • 2 comments

Bug report

The following code does not work:

class SlotNonDataclass:
    __slots__ = ("foo",)

    def __init__(self, foo: str):
        # To support frozen derived classes.
        object.__setattr__(self, "foo", foo)

@dataclass(frozen=True)
class FrozenDerivingSlotsClass(SlotNonDataclass):
    bar: int

    def __init__(self, foo: str, bar: int):
        # super() without arguments does not work with slots=True,
        # as the actual class is different from the one we have here.
        super(TestFrozen.FrozenDerivingSlotsClass, self).__init__(foo)
        object.__setattr__(self, "bar", bar)

obj = FrozenDerivingSlotsClass("a", 1)
p = pickle.loads(pickle.dumps(obj))

assert obj.foo == p.foo
assert obj.bar == p.bar

Linked PRs

  • gh-105878

avboag avatar Jun 16 '23 23:06 avboag

ping

avboag avatar Mar 18 '24 21:03 avboag

ping

avboag avatar May 10 '24 20:05 avboag

ping

avboag avatar Jul 09 '24 15:07 avboag