mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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.
This commit is contained in:
parent
f757b72b25
commit
56970b8ce9
3 changed files with 66 additions and 17 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue