mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
gh-109409: Fix inheritance of frozen dataclass from non-frozen dataclass mixins (gh-109437)
Fix inheritance of frozen dataclass from non-frozen dataclass mixins
This commit is contained in:
parent
7dd3c2b800
commit
b6000d2874
3 changed files with 107 additions and 4 deletions
|
@ -2863,6 +2863,101 @@ class TestFrozen(unittest.TestCase):
|
|||
class D(C):
|
||||
j: int
|
||||
|
||||
def test_inherit_frozen_mutliple_inheritance(self):
|
||||
@dataclass
|
||||
class NotFrozen:
|
||||
pass
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Frozen:
|
||||
pass
|
||||
|
||||
class NotDataclass:
|
||||
pass
|
||||
|
||||
for bases in (
|
||||
(NotFrozen, Frozen),
|
||||
(Frozen, NotFrozen),
|
||||
(Frozen, NotDataclass),
|
||||
(NotDataclass, Frozen),
|
||||
):
|
||||
with self.subTest(bases=bases):
|
||||
with self.assertRaisesRegex(
|
||||
TypeError,
|
||||
'cannot inherit non-frozen dataclass from a frozen one',
|
||||
):
|
||||
@dataclass
|
||||
class NotFrozenChild(*bases):
|
||||
pass
|
||||
|
||||
for bases in (
|
||||
(NotFrozen, Frozen),
|
||||
(Frozen, NotFrozen),
|
||||
(NotFrozen, NotDataclass),
|
||||
(NotDataclass, NotFrozen),
|
||||
):
|
||||
with self.subTest(bases=bases):
|
||||
with self.assertRaisesRegex(
|
||||
TypeError,
|
||||
'cannot inherit frozen dataclass from a non-frozen one',
|
||||
):
|
||||
@dataclass(frozen=True)
|
||||
class FrozenChild(*bases):
|
||||
pass
|
||||
|
||||
def test_inherit_frozen_mutliple_inheritance_regular_mixins(self):
|
||||
@dataclass(frozen=True)
|
||||
class Frozen:
|
||||
pass
|
||||
|
||||
class NotDataclass:
|
||||
pass
|
||||
|
||||
class C1(Frozen, NotDataclass):
|
||||
pass
|
||||
self.assertEqual(C1.__mro__, (C1, Frozen, NotDataclass, object))
|
||||
|
||||
class C2(NotDataclass, Frozen):
|
||||
pass
|
||||
self.assertEqual(C2.__mro__, (C2, NotDataclass, Frozen, object))
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class C3(Frozen, NotDataclass):
|
||||
pass
|
||||
self.assertEqual(C3.__mro__, (C3, Frozen, NotDataclass, object))
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class C4(NotDataclass, Frozen):
|
||||
pass
|
||||
self.assertEqual(C4.__mro__, (C4, NotDataclass, Frozen, object))
|
||||
|
||||
def test_multiple_frozen_dataclasses_inheritance(self):
|
||||
@dataclass(frozen=True)
|
||||
class FrozenA:
|
||||
pass
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FrozenB:
|
||||
pass
|
||||
|
||||
class C1(FrozenA, FrozenB):
|
||||
pass
|
||||
self.assertEqual(C1.__mro__, (C1, FrozenA, FrozenB, object))
|
||||
|
||||
class C2(FrozenB, FrozenA):
|
||||
pass
|
||||
self.assertEqual(C2.__mro__, (C2, FrozenB, FrozenA, object))
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class C3(FrozenA, FrozenB):
|
||||
pass
|
||||
self.assertEqual(C3.__mro__, (C3, FrozenA, FrozenB, object))
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class C4(FrozenB, FrozenA):
|
||||
pass
|
||||
self.assertEqual(C4.__mro__, (C4, FrozenB, FrozenA, object))
|
||||
|
||||
def test_inherit_nonfrozen_from_empty(self):
|
||||
@dataclass
|
||||
class C:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue