Repository navigation
[mypyc] Fix pickling and copying frozen dataclasses - #22127
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes mypyc/mypyc#1213.
A compiled frozen dataclass can't be pickled or copied, so it can't be sent to a
multiprocessingworker either:The
__setstate__that mypyc gives native classes restored each attribute withPyObject_SetAttr, which calls the class's__setattr__, and the one@dataclass(frozen=True)adds always raises.CPython restores the state of an object by updating its
__dict__directly, without calling__setattr__, and for a frozen dataclass withslots=True,dataclassesadds a__setstate__that usesobject.__setattr__. Now__setstate__sets the attributes withPyObject_GenericSetAttr, asobject.__setattr__does. The copies are still frozen.This only changes anything for classes with their own
__setattr__, sincePyObject_GenericSetAttris already thetp_setattroof the others. A native class that defines__setattr__no longer has it called when an instance is unpickled or copied, which matches CPython for a class without__slots__.