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.
(cherry picked from commit 56970b8ce9d23269d20a76f13c80e670c856ba7f)
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
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 @@
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 @@
'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