Skip to content

[3.10] bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692) #29704

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

Merged
merged 1 commit into from
Nov 22, 2021

Conversation

miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Nov 22, 2021

Special handling is needed, because for non-slots dataclasses the instance attributes are not set: reading from a field just references the class's attribute of the same name, which contains the default value. But this doesn't work for classes using __slots__: they don't read the class's attribute. So in that case (and that case only), initialize the instance attribute. Handle this for both normal defaults, and for fields using default_factory.
(cherry picked from commit d3062f6)

Co-authored-by: Eric V. Smith [email protected]

https://bugs.python.org/issue44649

Automerge-Triggered-By: GH:ericvsmith

…ut init=False (pythonGH-29692)

Special handling is needed, because for non-slots dataclasses the instance attributes are not set: reading from a field just references the class's attribute of the same name, which contains the default value. But this doesn't work for classes using __slots__: they don't read the class's attribute. So in that case (and that case only), initialize the instance attribute. Handle this for both normal defaults, and for fields using default_factory.
(cherry picked from commit d3062f6)

Co-authored-by: Eric V. Smith <[email protected]>
@miss-islington
Copy link
Contributor Author

Sorry, I can't merge this PR. Reason: 3 of 6 required status checks are expected..

@miss-islington
Copy link
Contributor Author

@ericvsmith: Status check is done, and it's a success ✅ .

@miss-islington miss-islington merged commit 10343bd into python:3.10 Nov 22, 2021
@miss-islington miss-islington deleted the backport-d3062f6-3.10 branch November 22, 2021 13:47
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.

4 participants