Skip to content

Commit d0c5d3b

Browse files
committed
Include the new test for init annotations in all formats
1 parent 5820f94 commit d0c5d3b

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lib/test/test_dataclasses/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,6 +2470,37 @@ def __init__(self, a):
24702470

24712471
self.assertEqual(D(5).a, 10)
24722472

2473+
def test_annotate_function(self):
2474+
# Test that the __init__ function has correct annotate function
2475+
# See: https://github.com/python/cpython/issues/137530
2476+
# With no forward references
2477+
@dataclass
2478+
class A:
2479+
a: int
2480+
2481+
value_annos = annotationlib.get_annotations(A.__init__, format=annotationlib.Format.VALUE)
2482+
forwardref_annos = annotationlib.get_annotations(A.__init__, format=annotationlib.Format.FORWARDREF)
2483+
string_annos = annotationlib.get_annotations(A.__init__, format=annotationlib.Format.STRING)
2484+
2485+
self.assertEqual(value_annos, {'a': int, 'return': None})
2486+
self.assertEqual(forwardref_annos, {'a': int, 'return': None})
2487+
self.assertEqual(string_annos, {'a': 'int', 'return': 'None'})
2488+
2489+
# With forward references
2490+
@dataclass
2491+
class B:
2492+
b: undefined
2493+
2494+
# VALUE annotations should raise
2495+
with self.assertRaises(NameError):
2496+
_ = annotationlib.get_annotations(B.__init__, format=annotationlib.Format.VALUE)
2497+
2498+
forwardref_annos = annotationlib.get_annotations(B.__init__, format=annotationlib.Format.FORWARDREF)
2499+
string_annos = annotationlib.get_annotations(B.__init__, format=annotationlib.Format.STRING)
2500+
2501+
self.assertEqual(forwardref_annos, {'b': support.EqualToForwardRef('undefined', owner=B, is_class=True), 'return': None})
2502+
self.assertEqual(string_annos, {'b': 'undefined', 'return': 'None'})
2503+
24732504

24742505
class TestRepr(unittest.TestCase):
24752506
def test_repr(self):

0 commit comments

Comments
 (0)