Mailing List Archive

bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692)
https://github.com/python/cpython/commit/d3062f672c92855b7e9e962ad4bf1a67abd4589b
commit: d3062f672c92855b7e9e962ad4bf1a67abd4589b
branch: main
author: Eric V. Smith <ericvsmith@users.noreply.github.com>
committer: ericvsmith <ericvsmith@users.noreply.github.com>
date: 2021-11-22T08:26:12-05:00
summary:

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.

files:
A Misc/NEWS.d/next/Library/2021-11-21-20-50-42.bpo-44649.E8M936.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index aca60501d0e0f..8643589077a4a 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -447,7 +447,7 @@ def _field_assign(frozen, name, value, self_name):
return f'{self_name}.{name}={value}'


-def _field_init(f, frozen, globals, self_name):
+def _field_init(f, frozen, globals, self_name, slots):
# Return the text of the line in the body of __init__ that will
# initialize this field.

@@ -487,9 +487,15 @@ def _field_init(f, frozen, globals, self_name):
globals[default_name] = f.default
value = f.name
else:
- # This field does not need initialization. Signify that
- # to the caller by returning None.
- return None
+ # If the class has slots, then initialize this field.
+ if slots and f.default is not MISSING:
+ globals[default_name] = f.default
+ value = default_name
+ else:
+ # This field does not need initialization: reading from it will
+ # just use the class attribute that contains the default.
+ # Signify that to the caller by returning None.
+ return None

# Only test this now, so that we can create variables for the
# default. However, return None to signify that we're not going
@@ -521,7 +527,7 @@ def _init_param(f):


def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,
- self_name, globals):
+ self_name, globals, slots):
# fields contains both real fields and InitVar pseudo-fields.

# Make sure we don't have fields without defaults following fields
@@ -548,7 +554,7 @@ def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,

body_lines = []
for f in fields:
- line = _field_init(f, frozen, locals, self_name)
+ line = _field_init(f, frozen, locals, self_name, slots)
# line is None means that this field doesn't require
# initialization (it's a pseudo-field). Just skip it.
if line:
@@ -1027,6 +1033,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
'__dataclass_self__' if 'self' in fields
else 'self',
globals,
+ slots,
))

# Get the fields as a list, and include only real fields. This is
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index b00d0484d387e..bcd004f4ec3aa 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -2880,6 +2880,28 @@ def test_frozen_pickle(self):
self.assertIsNot(obj, p)
self.assertEqual(obj, p)

+ def test_slots_with_default_no_init(self):
+ # Originally reported in bpo-44649.
+ @dataclass(slots=True)
+ class A:
+ a: str
+ b: str = field(default='b', init=False)
+
+ obj = A("a")
+ self.assertEqual(obj.a, 'a')
+ self.assertEqual(obj.b, 'b')
+
+ def test_slots_with_default_factory_no_init(self):
+ # Originally reported in bpo-44649.
+ @dataclass(slots=True)
+ class A:
+ a: str
+ b: str = field(default_factory=lambda:'b', init=False)
+
+ obj = A("a")
+ self.assertEqual(obj.a, 'a')
+ self.assertEqual(obj.b, 'b')
+
class TestDescriptors(unittest.TestCase):
def test_set_name(self):
# See bpo-33141.
diff --git a/Misc/NEWS.d/next/Library/2021-11-21-20-50-42.bpo-44649.E8M936.rst b/Misc/NEWS.d/next/Library/2021-11-21-20-50-42.bpo-44649.E8M936.rst
new file mode 100644
index 0000000000000..f6391a915a821
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-11-21-20-50-42.bpo-44649.E8M936.rst
@@ -0,0 +1,2 @@
+Handle dataclass(slots=True) with a field that has default a default value,
+but for which init=False.

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins
bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692) [ In reply to ]
https://github.com/python/cpython/commit/10343bd98390ef15909e3a19f26a6178162996fd
commit: 10343bd98390ef15909e3a19f26a6178162996fd
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
committer: miss-islington <31488909+miss-islington@users.noreply.github.com>
date: 2021-11-22T05:47:41-08:00
summary:

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>

files:
A Misc/NEWS.d/next/Library/2021-11-21-20-50-42.bpo-44649.E8M936.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 4f4aa3d3487ce..b3a9194d9d068 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -447,7 +447,7 @@ def _field_assign(frozen, name, value, self_name):
return f'{self_name}.{name}={value}'


-def _field_init(f, frozen, globals, self_name):
+def _field_init(f, frozen, globals, self_name, slots):
# Return the text of the line in the body of __init__ that will
# initialize this field.

@@ -487,9 +487,15 @@ def _field_init(f, frozen, globals, self_name):
globals[default_name] = f.default
value = f.name
else:
- # This field does not need initialization. Signify that
- # to the caller by returning None.
- return None
+ # If the class has slots, then initialize this field.
+ if slots and f.default is not MISSING:
+ globals[default_name] = f.default
+ value = default_name
+ else:
+ # This field does not need initialization: reading from it will
+ # just use the class attribute that contains the default.
+ # Signify that to the caller by returning None.
+ return None

# Only test this now, so that we can create variables for the
# default. However, return None to signify that we're not going
@@ -521,7 +527,7 @@ def _init_param(f):


def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,
- self_name, globals):
+ self_name, globals, slots):
# fields contains both real fields and InitVar pseudo-fields.

# Make sure we don't have fields without defaults following fields
@@ -548,7 +554,7 @@ def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,

body_lines = []
for f in fields:
- line = _field_init(f, frozen, locals, self_name)
+ line = _field_init(f, frozen, locals, self_name, slots)
# line is None means that this field doesn't require
# initialization (it's a pseudo-field). Just skip it.
if line:
@@ -1027,6 +1033,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
'__dataclass_self__' if 'self' in fields
else 'self',
globals,
+ slots,
))

# Get the fields as a list, and include only real fields. This is
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index b00d0484d387e..bcd004f4ec3aa 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -2880,6 +2880,28 @@ def test_frozen_pickle(self):
self.assertIsNot(obj, p)
self.assertEqual(obj, p)

+ def test_slots_with_default_no_init(self):
+ # Originally reported in bpo-44649.
+ @dataclass(slots=True)
+ class A:
+ a: str
+ b: str = field(default='b', init=False)
+
+ obj = A("a")
+ self.assertEqual(obj.a, 'a')
+ self.assertEqual(obj.b, 'b')
+
+ def test_slots_with_default_factory_no_init(self):
+ # Originally reported in bpo-44649.
+ @dataclass(slots=True)
+ class A:
+ a: str
+ b: str = field(default_factory=lambda:'b', init=False)
+
+ obj = A("a")
+ self.assertEqual(obj.a, 'a')
+ self.assertEqual(obj.b, 'b')
+
class TestDescriptors(unittest.TestCase):
def test_set_name(self):
# See bpo-33141.
diff --git a/Misc/NEWS.d/next/Library/2021-11-21-20-50-42.bpo-44649.E8M936.rst b/Misc/NEWS.d/next/Library/2021-11-21-20-50-42.bpo-44649.E8M936.rst
new file mode 100644
index 0000000000000..f6391a915a821
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-11-21-20-50-42.bpo-44649.E8M936.rst
@@ -0,0 +1,2 @@
+Handle dataclass(slots=True) with a field that has default a default value,
+but for which init=False.

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins