Skip to content

Commit d3062f6

Browse files
authored
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.
1 parent 5b946ca commit d3062f6

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

Lib/dataclasses.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def _field_assign(frozen, name, value, self_name):
447447
return f'{self_name}.{name}={value}'
448448

449449

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

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

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

522528

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

527533
# 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,
548554

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

10321039
# Get the fields as a list, and include only real fields. This is

Lib/test/test_dataclasses.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,6 +2880,28 @@ def test_frozen_pickle(self):
28802880
self.assertIsNot(obj, p)
28812881
self.assertEqual(obj, p)
28822882

2883+
def test_slots_with_default_no_init(self):
2884+
# Originally reported in bpo-44649.
2885+
@dataclass(slots=True)
2886+
class A:
2887+
a: str
2888+
b: str = field(default='b', init=False)
2889+
2890+
obj = A("a")
2891+
self.assertEqual(obj.a, 'a')
2892+
self.assertEqual(obj.b, 'b')
2893+
2894+
def test_slots_with_default_factory_no_init(self):
2895+
# Originally reported in bpo-44649.
2896+
@dataclass(slots=True)
2897+
class A:
2898+
a: str
2899+
b: str = field(default_factory=lambda:'b', init=False)
2900+
2901+
obj = A("a")
2902+
self.assertEqual(obj.a, 'a')
2903+
self.assertEqual(obj.b, 'b')
2904+
28832905
class TestDescriptors(unittest.TestCase):
28842906
def test_set_name(self):
28852907
# See bpo-33141.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Handle dataclass(slots=True) with a field that has default a default value,
2+
but for which init=False.

0 commit comments

Comments
 (0)