diff options
author | Yurii Karabas <1998uriyyo@gmail.com> | 2021-05-01 02:14:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-01 02:14:30 (GMT) |
commit | c24199184bea0c851c1a7296ae54aaf18ee56752 (patch) | |
tree | daf34100898317581770db30eeb4190acd638a59 /Lib/test/test_dataclasses.py | |
parent | 558df9010915c8fe94f4d7f842e7c5aabbb06b14 (diff) | |
download | cpython-c24199184bea0c851c1a7296ae54aaf18ee56752.zip cpython-c24199184bea0c851c1a7296ae54aaf18ee56752.tar.gz cpython-c24199184bea0c851c1a7296ae54aaf18ee56752.tar.bz2 |
bpo-42269: Add slots parameter to dataclass decorator (GH-24171)
Add slots parameter to dataclass decorator and make_dataclass function.
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r-- | Lib/test/test_dataclasses.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 670648a..2fa0ae0 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -2781,6 +2781,59 @@ class TestSlots(unittest.TestCase): # We can add a new field to the derived instance. d.z = 10 + def test_generated_slots(self): + @dataclass(slots=True) + class C: + x: int + y: int + + c = C(1, 2) + self.assertEqual((c.x, c.y), (1, 2)) + + c.x = 3 + c.y = 4 + self.assertEqual((c.x, c.y), (3, 4)) + + with self.assertRaisesRegex(AttributeError, "'C' object has no attribute 'z'"): + c.z = 5 + + def test_add_slots_when_slots_exists(self): + with self.assertRaisesRegex(TypeError, '^C already specifies __slots__$'): + @dataclass(slots=True) + class C: + __slots__ = ('x',) + x: int + + def test_generated_slots_value(self): + @dataclass(slots=True) + class Base: + x: int + + self.assertEqual(Base.__slots__, ('x',)) + + @dataclass(slots=True) + class Delivered(Base): + y: int + + self.assertEqual(Delivered.__slots__, ('x', 'y')) + + @dataclass + class AnotherDelivered(Base): + z: int + + self.assertTrue('__slots__' not in AnotherDelivered.__dict__) + + def test_returns_new_class(self): + class A: + x: int + + B = dataclass(A, slots=True) + self.assertIsNot(A, B) + + self.assertFalse(hasattr(A, "__slots__")) + self.assertTrue(hasattr(B, "__slots__")) + + class TestDescriptors(unittest.TestCase): def test_set_name(self): # See bpo-33141. |