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 56970b8ce9)

Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2018-03-22 13:58:59 -07:00 committed by GitHub
parent 5d8bb5d07b
commit 3b4c6b16c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 17 deletions

View file

@ -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