Repository navigation
fix(slots): preserve __dict__ state when pickling slotted classes - #1643
Open
saadkhantareen wants to merge 1 commit into
Open
saadkhantareen wants to merge 1 commit into
saadkhantareen wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Summary
A slotted attrs class that inherits from a non-slotted class has an instance
__dict__, but the generated__getstate__/__setstate__only handled the attrs fields.Any additional state stored in that
__dict__-- for example an attribute set in__attrs_post_init__or by a mixin -- was silently dropped when the instance was pickled (which also breaksmultiprocessing, as reported in #1020) or copied.This change makes the generated state handling preserve that
__dict__state, mirroring whatobject.__getstate__()returns for such classes, while keeping the existing state format.Changes
src/attr/_make.py: in_ClassBuilder._make_getstate_setstate(),slots_getstatenow merges entries from the instance__dict__that are not attrs fields into the returned state dict (attrs fields take precedence).slots_setstaterestores those extra entries into the instance__dict__when the instance has one. Instances without a__dict__ignore extra keys as before, so unpickling after an attribute was removed from a slotted class keeps working, and both the pre-22.2.0 tuple state format and the current dict format remain readable.tests/test_slots.py: added a slotted subclass of a non-slotted attrs class whose__attrs_post_init__stores an extra attribute in__dict__, with regression tests covering a pickle round-trip for every protocol andcopy.copy.changelog.d/1020.change.md: added a news fragment.Tests
PYTHONPATH=src python -m pytest tests/test_slots.py -q-- 59 passed, 1 skippedPYTHONPATH=src python -m pytest tests/test_dunders.py tests/test_funcs.py -q-- 152 passedruff format --check src/attr/_make.py tests/test_slots.pyandruff check src/attr/_make.py tests/test_slots.py-- cleanFixes #1020
Pull Request Checklist
mainbranch..pyi).typing_tests/baseline.pyor, if necessary,typing_tests/mypy.py.attr/__init__.pyi, they've also been re-imported inattrs/__init__.pyi.docs/api.rstby hand.@attr.s()and@attrs.define()have to be added by hand too.versionadded,versionchanged, ordeprecateddirectives..rstand.mdfiles is written using semantic newlines.changelog.d.