diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2018-03-22 20:28:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-22 20:28:48 (GMT) |
commit | 56970b8ce9d23269d20a76f13c80e670c856ba7f (patch) | |
tree | 53012d578fa3d64d9919453bb817e89a94227198 /Lib/test/test_dataclasses.py | |
parent | f757b72b2524ce3451d2269f0b8a9f0593a7b27f (diff) | |
download | cpython-56970b8ce9d23269d20a76f13c80e670c856ba7f.zip cpython-56970b8ce9d23269d20a76f13c80e670c856ba7f.tar.gz cpython-56970b8ce9d23269d20a76f13c80e670c856ba7f.tar.bz2 |
bpo-32505: dataclasses: raise TypeError if a member variable is of type Field, but doesn't have a type annotation. (GH-6192)
If a dataclass has a member variable that's of type Field, but it doesn't have a type annotation, raise TypeError.
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rwxr-xr-x | Lib/test/test_dataclasses.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 69ace36..8aff8ae 100755 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -24,6 +24,14 @@ class TestCase(unittest.TestCase): o = C() self.assertEqual(len(fields(C)), 0) + def test_no_fields_but_member_variable(self): + @dataclass + class C: + i = 0 + + o = C() + self.assertEqual(len(fields(C)), 0) + def test_one_field_no_default(self): @dataclass class C: @@ -1906,6 +1914,41 @@ class TestCase(unittest.TestCase): 'z': 'typing.Any'}) +class TestFieldNoAnnotation(unittest.TestCase): + def test_field_without_annotation(self): + with self.assertRaisesRegex(TypeError, + "'f' is a field but has no type annotation"): + @dataclass + class C: + f = field() + + def test_field_without_annotation_but_annotation_in_base(self): + @dataclass + class B: + f: int + + with self.assertRaisesRegex(TypeError, + "'f' is a field but has no type annotation"): + # This is still an error: make sure we don't pick up the + # type annotation in the base class. + @dataclass + class C(B): + f = field() + + def test_field_without_annotation_but_annotation_in_base_not_dataclass(self): + # Same test, but with the base class not a dataclass. + class B: + f: int + + with self.assertRaisesRegex(TypeError, + "'f' is a field but has no type annotation"): + # This is still an error: make sure we don't pick up the + # type annotation in the base class. + @dataclass + class C(B): + f = field() + + class TestDocString(unittest.TestCase): def assertDocStrEqual(self, a, b): # Because 3.6 and 3.7 differ in how inspect.signature work |