mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
bpo-43176: Fix processing of empty dataclasses (GH-24484) (GH-25205)
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
(cherry picked from commit 376ffc6ac4
)
Co-authored-by: Iurii Kemaev <6885137+hbq1@users.noreply.github.com>
Co-authored-by: Iurii Kemaev <6885137+hbq1@users.noreply.github.com>
This commit is contained in:
parent
c7ccb0ff61
commit
8a34a0793b
3 changed files with 26 additions and 1 deletions
|
@ -836,7 +836,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
|
||||||
# Only process classes that have been processed by our
|
# Only process classes that have been processed by our
|
||||||
# decorator. That is, they have a _FIELDS attribute.
|
# decorator. That is, they have a _FIELDS attribute.
|
||||||
base_fields = getattr(b, _FIELDS, None)
|
base_fields = getattr(b, _FIELDS, None)
|
||||||
if base_fields:
|
if base_fields is not None:
|
||||||
has_dataclass_bases = True
|
has_dataclass_bases = True
|
||||||
for f in base_fields.values():
|
for f in base_fields.values():
|
||||||
fields[f.name] = f
|
fields[f.name] = f
|
||||||
|
|
|
@ -2568,6 +2568,30 @@ class TestFrozen(unittest.TestCase):
|
||||||
self.assertEqual(d.i, 0)
|
self.assertEqual(d.i, 0)
|
||||||
self.assertEqual(d.j, 10)
|
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)
|
# Test both ways: with an intermediate normal (non-dataclass)
|
||||||
# class and without an intermediate class.
|
# class and without an intermediate class.
|
||||||
def test_inherit_nonfrozen_from_frozen(self):
|
def test_inherit_nonfrozen_from_frozen(self):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fixed processing of empty dataclasses.
|
Loading…
Add table
Add a link
Reference in a new issue