Skip to content

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

Merged
miss-islington merged 1 commit into
python:3.10from
miss-islington:backport-d3062f6-3.10
Nov 22, 2021
Merged

[3.10] bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692)#29704
miss-islington merged 1 commit into
python:3.10from
miss-islington:backport-d3062f6-3.10

Conversation

@miss-islington

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

Copy link
Copy Markdown
Contributor

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

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 <ericvsmith@users.noreply.github.com>
@miss-islington

Copy link
Copy Markdown
Contributor Author

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

@miss-islington

Copy link
Copy Markdown
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