bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False by ericvsmith · Pull Request #29692 · python/cpython (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation2 Commits2 Checks0 Files changed

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

[ Show hidden characters]({{ revealButtonHref }})

ericvsmith

@ericvsmith

@ericvsmith

@miss-islington

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request

Nov 22, 2021

@ericvsmith @miss-islington

…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

miss-islington added a commit that referenced this pull request

Nov 22, 2021

@miss-islington @ericvsmith

…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

@ericvsmith @remykarem

…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.