diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2018-02-26 20:38:33 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-26 20:38:33 -0500 |
commit | 2fa6b9eae07e2385e2acbf2e40093a21fb3a10c4 (patch) | |
tree | 8f5dfd3eb970c93199bd916e3b7d3dbd02dcf262 /Lib/test/test_dataclasses.py | |
parent | 72d9b2be36f091793ae7ffc5ad751f040c6e6ad3 (diff) | |
download | cpython-git-2fa6b9eae07e2385e2acbf2e40093a21fb3a10c4.tar.gz |
bpo-32960: For dataclasses, disallow inheriting frozen from non-frozen classes and vice-versa, (GH-5919)
This restriction will be relaxed at a future date.
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rwxr-xr-x | Lib/test/test_dataclasses.py | 84 |
1 files changed, 61 insertions, 23 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 582cb3459f..46d485c015 100755 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -637,29 +637,6 @@ class TestCase(unittest.TestCase): y: int self.assertNotEqual(Point(1, 3), C(1, 3)) - def test_frozen(self): - @dataclass(frozen=True) - class C: - i: int - - c = C(10) - self.assertEqual(c.i, 10) - with self.assertRaises(FrozenInstanceError): - c.i = 5 - self.assertEqual(c.i, 10) - - # Check that a derived class is still frozen, even if not - # marked so. - @dataclass - class D(C): - pass - - d = D(20) - self.assertEqual(d.i, 20) - with self.assertRaises(FrozenInstanceError): - d.i = 5 - self.assertEqual(d.i, 20) - def test_not_tuple(self): # Test that some of the problems with namedtuple don't happen # here. @@ -2475,5 +2452,66 @@ class TestHash(unittest.TestCase): assert False, f'unknown value for expected={expected!r}' +class TestFrozen(unittest.TestCase): + def test_frozen(self): + @dataclass(frozen=True) + class C: + i: int + + c = C(10) + self.assertEqual(c.i, 10) + with self.assertRaises(FrozenInstanceError): + c.i = 5 + self.assertEqual(c.i, 10) + + def test_inherit(self): + @dataclass(frozen=True) + class C: + i: int + + @dataclass(frozen=True) + class D(C): + j: int + + d = D(0, 10) + with self.assertRaises(FrozenInstanceError): + d.i = 5 + self.assertEqual(d.i, 0) + + def test_inherit_from_nonfrozen_from_frozen(self): + @dataclass(frozen=True) + class C: + i: int + + with self.assertRaisesRegex(TypeError, + 'cannot inherit non-frozen dataclass from a frozen one'): + @dataclass + class D(C): + pass + + def test_inherit_from_frozen_from_nonfrozen(self): + @dataclass + class C: + i: int + + with self.assertRaisesRegex(TypeError, + 'cannot inherit frozen dataclass from a non-frozen one'): + @dataclass(frozen=True) + class D(C): + pass + + def test_inherit_from_normal_class(self): + class C: + pass + + @dataclass(frozen=True) + class D(C): + i: int + + d = D(10) + with self.assertRaises(FrozenInstanceError): + d.i = 5 + + if __name__ == '__main__': unittest.main() |