]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 22 Nov 2021 13:47:41 +0000 (05:47 -0800)
committerGitHub <noreply@github.com>
Mon, 22 Nov 2021 13:47:41 +0000 (05:47 -0800)
commit10343bd98390ef15909e3a19f26a6178162996fd
tree4379892af918e04bf8756a7daa111a4b1233b54e
parent9e7a2e492052956d511d753a276a4bdf6eb47072
bpo-44649: Fix dataclasses(slots=True) with a field with a default, but 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 d3062f672c92855b7e9e962ad4bf1a67abd4589b)

Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
Lib/dataclasses.py
Lib/test/test_dataclasses.py
Misc/NEWS.d/next/Library/2021-11-21-20-50-42.bpo-44649.E8M936.rst [new file with mode: 0644]