diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2018-03-19 21:07:51 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-19 21:07:51 -0400 |
commit | 7389fd935c95b4b6f094312294e703ee0de18719 (patch) | |
tree | db6bb8cbf62b96d09d532d21f3bd939e1b64e8b0 /Lib/test/test_dataclasses.py | |
parent | 4573820d2a9156346392838d455e89f33067e9dd (diff) | |
download | cpython-git-7389fd935c95b4b6f094312294e703ee0de18719.tar.gz |
bpo-33100: Dataclasses now handles __slots__ and default values correctly. (GH-6152)
If the class has a member that's a MemberDescriptorType, it's not a default value, it's from that member being in __slots__.
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rwxr-xr-x | Lib/test/test_dataclasses.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 3e67263609..db03ec1925 100755 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -2564,5 +2564,47 @@ class TestFrozen(unittest.TestCase): self.assertEqual(s.cached, True) +class TestSlots(unittest.TestCase): + def test_simple(self): + @dataclass + class C: + __slots__ = ('x',) + x: Any + + # There was a bug where a variable in a slot was assumed + # to also have a default value (of type types.MemberDescriptorType). + with self.assertRaisesRegex(TypeError, + "__init__\(\) missing 1 required positional argument: 'x'"): + C() + + # We can create an instance, and assign to x. + c = C(10) + self.assertEqual(c.x, 10) + c.x = 5 + self.assertEqual(c.x, 5) + + # We can't assign to anything else. + with self.assertRaisesRegex(AttributeError, "'C' object has no attribute 'y'"): + c.y = 5 + + def test_derived_added_field(self): + # See bpo-33100. + @dataclass + class Base: + __slots__ = ('x',) + x: Any + + @dataclass + class Derived(Base): + x: int + y: int + + d = Derived(1, 2) + self.assertEqual((d.x, d.y), (1, 2)) + + # We can add a new field to the derived instance. + d.z = 10 + + if __name__ == '__main__': unittest.main() |