Skip to content

Commit 376ffc6

Browse files
authored
bpo-43176: Fix processing of empty dataclasses (GH-24484)
When a dataclass inherits from an empty base, all immutability checks are omitted. This PR fixes this and adds tests for it. Automerge-Triggered-By: GH:ericvsmith
1 parent 4663e5f commit 376ffc6

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Lib/dataclasses.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
860860
# Only process classes that have been processed by our
861861
# decorator. That is, they have a _FIELDS attribute.
862862
base_fields = getattr(b, _FIELDS, None)
863-
if base_fields:
863+
if base_fields is not None:
864864
has_dataclass_bases = True
865865
for f in base_fields.values():
866866
fields[f.name] = f

Lib/test/test_dataclasses.py

+24
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,30 @@ class D(C):
25942594
self.assertEqual(d.i, 0)
25952595
self.assertEqual(d.j, 10)
25962596

2597+
def test_inherit_nonfrozen_from_empty_frozen(self):
2598+
@dataclass(frozen=True)
2599+
class C:
2600+
pass
2601+
2602+
with self.assertRaisesRegex(TypeError,
2603+
'cannot inherit non-frozen dataclass from a frozen one'):
2604+
@dataclass
2605+
class D(C):
2606+
j: int
2607+
2608+
def test_inherit_nonfrozen_from_empty(self):
2609+
@dataclass
2610+
class C:
2611+
pass
2612+
2613+
@dataclass
2614+
class D(C):
2615+
j: int
2616+
2617+
d = D(3)
2618+
self.assertEqual(d.j, 3)
2619+
self.assertIsInstance(d, C)
2620+
25972621
# Test both ways: with an intermediate normal (non-dataclass)
25982622
# class and without an intermediate class.
25992623
def test_inherit_nonfrozen_from_frozen(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed processing of empty dataclasses.

0 commit comments

Comments
 (0)