You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Frozen dataclasses with slots cannot override __getstate__ or __setstate__ #104035
You are supposed to be able to override how pickle pickles an object with __getstate__ and __setstate__. However, these methods are ignored in a dataclass when specifying both frozen=True and slots=True.
The expected "getstate" and "setstate" lines are never printed because the supplied methods are never called. If either frozen or slots is removed, the expected lines are printed.
From the source code, it is pretty clear why this is happening. If frozen and slots are both True, then special versions of __getstate__ and __setstate__ are unconditionally attached to the dataclass. The dataclass decorator should probably treat this way that it does other methods—only add a method if it is not already present.
It does seem reasonable to ignore __getstate__ and __setstate__ if they're already set. Have you tested this? And in particular, does it break any existing tests?
Activity
ericvsmith commentedon May 1, 2023
It does seem reasonable to ignore
__getstate__and__setstate__if they're already set. Have you tested this? And in particular, does it break any existing tests?pythongh-104035: Do not ignore user-defined `__{get,set}state__` in s…
sobolevn commentedon May 1, 2023
Oh, I just went for fixing this in #104041 :)
New logic will allow users to customize
__getstate__and__setstate__method.Thanks a lot, @drhagen for finding this :)
It is funny that I've spent the whole day optimizing these two in #103032, but I haven't seen this! 👍
gh-104035: Do not ignore user-defined `__{get,set}state__` in slotted…
pythongh-104035: Do not ignore user-defined `__{get,set}state__` in s…
[3.11] gh-104035: Do not ignore user-defined `__{get,set}state__` in …
Merge branch 'main' into inlinecomp2