Skip to content

[3.9] bpo-43176: Fix processing of empty dataclasses (GH-24484) #25205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
# Only process classes that have been processed by our
# decorator. That is, they have a _FIELDS attribute.
base_fields = getattr(b, _FIELDS, None)
if base_fields:
if base_fields is not None:
has_dataclass_bases = True
for f in base_fields.values():
fields[f.name] = f
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2568,6 +2568,30 @@ class D(C):
self.assertEqual(d.i, 0)
self.assertEqual(d.j, 10)

def test_inherit_nonfrozen_from_empty_frozen(self):
@dataclass(frozen=True)
class C:
pass

with self.assertRaisesRegex(TypeError,
'cannot inherit non-frozen dataclass from a frozen one'):
@dataclass
class D(C):
j: int

def test_inherit_nonfrozen_from_empty(self):
@dataclass
class C:
pass

@dataclass
class D(C):
j: int

d = D(3)
self.assertEqual(d.j, 3)
self.assertIsInstance(d, C)

# Test both ways: with an intermediate normal (non-dataclass)
# class and without an intermediate class.
def test_inherit_nonfrozen_from_frozen(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed processing of empty dataclasses.