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

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

Merged
merged 2 commits into from Nov 22, 2021

Conversation

ericvsmith
Copy link
Member

@ericvsmith ericvsmith commented Nov 22, 2021

@ericvsmith ericvsmith self-assigned this Nov 22, 2021
@ericvsmith ericvsmith added the needs backport to 3.10 only security fixes label Nov 22, 2021
@ericvsmith ericvsmith merged commit d3062f6 into python:main Nov 22, 2021
@miss-islington
Copy link
Contributor

Thanks @ericvsmith for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

@ericvsmith ericvsmith deleted the bpo-44649 branch November 22, 2021 13:26
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 22, 2021
…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 <ericvsmith@users.noreply.github.com>
@bedevere-bot
Copy link

GH-29704 is a backport of this pull request to the 3.10 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Nov 22, 2021
miss-islington added a commit that referenced this pull request Nov 22, 2021
…ut init=False (GH-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 <ericvsmith@users.noreply.github.com>
remykarem pushed a commit to remykarem/cpython that referenced this pull request Dec 7, 2021
…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.
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.

None yet

4 participants