diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2018-03-26 17:29:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-26 17:29:16 (GMT) |
commit | de7a2f04d6b9427d568fcb43b6f512f9b4c4bd84 (patch) | |
tree | 522d8689db0b51a68dc8a976acae8b3d77d2aaba /Lib/test | |
parent | 030345c0bfc2f76684666fe5c61e766ba5debfe6 (diff) | |
download | cpython-de7a2f04d6b9427d568fcb43b6f512f9b4c4bd84.zip cpython-de7a2f04d6b9427d568fcb43b6f512f9b4c4bd84.tar.gz cpython-de7a2f04d6b9427d568fcb43b6f512f9b4c4bd84.tar.bz2 |
bpo-33141: Have dataclasses.Field pass through __set_name__ to any default argument. (GH-6260)
This is part of PEP 487 and the descriptor protocol.
Diffstat (limited to 'Lib/test')
-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 f7f132c..2745eaf 100755 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -2698,6 +2698,48 @@ class TestSlots(unittest.TestCase): # We can add a new field to the derived instance. d.z = 10 +class TestDescriptors(unittest.TestCase): + def test_set_name(self): + # See bpo-33141. + + # Create a descriptor. + class D: + def __set_name__(self, owner, name): + self.name = name + def __get__(self, instance, owner): + if instance is not None: + return 1 + return self + + # This is the case of just normal descriptor behavior, no + # dataclass code is involved in initializing the descriptor. + @dataclass + class C: + c: int=D() + self.assertEqual(C.c.name, 'c') + + # Now test with a default value and init=False, which is the + # only time this is really meaningful. If not using + # init=False, then the descriptor will be overwritten, anyway. + @dataclass + class C: + c: int=field(default=D(), init=False) + self.assertEqual(C.c.name, 'c') + self.assertEqual(C().c, 1) + + def test_non_descriptor(self): + # PEP 487 says __set_name__ should work on non-descriptors. + # Create a descriptor. + + class D: + def __set_name__(self, owner, name): + self.name = name + + @dataclass + class C: + c: int=field(default=D(), init=False) + self.assertEqual(C.c.name, 'c') + if __name__ == '__main__': unittest.main() |