diff options
author | Arie Bovenberg <a.c.bovenberg@gmail.com> | 2022-03-19 21:01:17 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-19 21:01:17 (GMT) |
commit | 82e9b0bb0ac44d4942b9e01b2cdd2ca85c17e563 (patch) | |
tree | c1cb2d3397dc3b907f8d19c7682a4703c4494d75 /Lib/test/test_dataclasses.py | |
parent | 383a3bec74f0bf0c1b1bef9e0048db389c618452 (diff) | |
download | cpython-82e9b0bb0ac44d4942b9e01b2cdd2ca85c17e563.zip cpython-82e9b0bb0ac44d4942b9e01b2cdd2ca85c17e563.tar.gz cpython-82e9b0bb0ac44d4942b9e01b2cdd2ca85c17e563.tar.bz2 |
bpo-46382 dataclass(slots=True) now takes inherited slots into account (GH-31980)
Do not include any members in __slots__ that are already in a base class's __slots__.
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r-- | Lib/test/test_dataclasses.py | 51 |
1 files changed, 43 insertions, 8 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 2f37ecd..847bcd4 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -2926,23 +2926,58 @@ class TestSlots(unittest.TestCase): x: int def test_generated_slots_value(self): - @dataclass(slots=True) - class Base: - x: int - self.assertEqual(Base.__slots__, ('x',)) + class Root: + __slots__ = {'x'} + + class Root2(Root): + __slots__ = {'k': '...', 'j': ''} + + class Root3(Root2): + __slots__ = ['h'] + + class Root4(Root3): + __slots__ = 'aa' @dataclass(slots=True) - class Delivered(Base): + class Base(Root4): y: int + j: str + h: str + + self.assertEqual(Base.__slots__, ('y', )) + + @dataclass(slots=True) + class Derived(Base): + aa: float + x: str + z: int + k: str + h: str - self.assertEqual(Delivered.__slots__, ('x', 'y')) + self.assertEqual(Derived.__slots__, ('z', )) @dataclass - class AnotherDelivered(Base): + class AnotherDerived(Base): z: int - self.assertTrue('__slots__' not in AnotherDelivered.__dict__) + self.assertNotIn('__slots__', AnotherDerived.__dict__) + + def test_cant_inherit_from_iterator_slots(self): + + class Root: + __slots__ = iter(['a']) + + class Root2(Root): + __slots__ = ('b', ) + + with self.assertRaisesRegex( + TypeError, + "^Slots of 'Root' cannot be determined" + ): + @dataclass(slots=True) + class C(Root2): + x: int def test_returns_new_class(self): class A: |