Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-125004: fix unpickling for dataclasses in a cycle via hashing #125005

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

charles-cooper
Copy link

@charles-cooper charles-cooper commented Oct 5, 2024

workaround for gh #124937 specific to dataclasses, following the recommended approach at
#124937 (comment).

fixes #125004

to reviewers:
i branched off of 3.11 since that is what i have on my machine. if you would like me to branch off of main instead, please let me know.
EDIT: never mind, i changed the base branch to main: #125005 (comment)

Copy link

cpython-cla-bot bot commented Oct 5, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Oct 5, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add test cases and a NEWS entry :)

workaround for gh python#124937, following the recommended
approach at
python#124937 (comment).
@bedevere-app
Copy link

bedevere-app bot commented Oct 5, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@charles-cooper
Copy link
Author

Please, add test cases and a NEWS entry :)

6a982b0
and
e3b3d45

Copy link
Member

@gpshead gpshead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pickling and compatibility is complicated, we'll definitely want some more eyeballs on this one. Leaving my initial thoughts while trying to understand it:

Lib/dataclasses.py Outdated Show resolved Hide resolved
Lib/dataclasses.py Outdated Show resolved Hide resolved
newcls.__getstate__ = _dataclass_getstate
if '__setstate__' not in cls_dict:
newcls.__setstate__ = _dataclass_setstate
if '__reduce__' not in cls_dict:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this keep the is_frozen condition? If not, can you explain why not? (nothing uses that arg any more if not, so remove the arg and update the caller if so)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, i don't think so, because we need the new reduce function no matter if the dataclass is frozen or not.

however, looking at the tests, it looks like maybe __getstate__ and __setstate__ are required if the dataclass has slots?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pondering... I wonder if always having a __reduce__ has meaningful performance implications?

@gpshead gpshead changed the title gh-125004: fix unpickling for dataclass objects gh-125004: fix unpickling for dataclasses in a cycle with a custom __hash__ Oct 5, 2024
@gpshead gpshead changed the title gh-125004: fix unpickling for dataclasses in a cycle with a custom __hash__ gh-125004: fix unpickling for dataclasses in a cycle via hashing Oct 5, 2024
@gpshead gpshead marked this pull request as draft October 5, 2024 17:48
@gpshead
Copy link
Member

gpshead commented Oct 5, 2024

FYI - I marked this PR as a Draft as tons of tests are failing.

@charles-cooper
Copy link
Author

FYI - I marked this PR as a Draft as tons of tests are failing.

Yep, makes sense. I wanted to get some feedback on the approach first. Also this is my first pull request to the repository, I don't have the build or test suite set up. What is the recommended way here to run tests locally?

@gpshead
Copy link
Member

gpshead commented Oct 6, 2024

Also this is my first pull request to the repository, I don't have the build or test suite set up. What is the recommended way here to run tests locally?

Check out our devguide, it should in theory answer everything like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dataclasses fail to unpickle when there is an object cycle with a custom __hash__ function
4 participants