Skip to content

Commit c7a4c56

Browse files
committed
Test that defining a forwardref makes init annotations resolve
1 parent b340a2f commit c7a4c56

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Lib/test/test_dataclasses/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2491,7 +2491,7 @@ class A:
24912491
class B:
24922492
b: undefined
24932493

2494-
# VALUE annotations should raise
2494+
# VALUE annotations should raise while unresolvable
24952495
with self.assertRaises(NameError):
24962496
_ = annotationlib.get_annotations(B.__init__, format=annotationlib.Format.VALUE)
24972497

@@ -2501,6 +2501,17 @@ class B:
25012501
self.assertEqual(forwardref_annos, {'b': support.EqualToForwardRef('undefined', owner=B, is_class=True), 'return': None})
25022502
self.assertEqual(string_annos, {'b': 'undefined', 'return': 'None'})
25032503

2504+
# Now VALUE and FORWARDREF should resolve, STRING should be unchanged
2505+
undefined = int
2506+
2507+
value_annos = annotationlib.get_annotations(B.__init__, format=annotationlib.Format.VALUE)
2508+
forwardref_annos = annotationlib.get_annotations(B.__init__, format=annotationlib.Format.FORWARDREF)
2509+
string_annos = annotationlib.get_annotations(B.__init__, format=annotationlib.Format.STRING)
2510+
2511+
self.assertEqual(value_annos, {'b': int, 'return': None})
2512+
self.assertEqual(forwardref_annos, {'b': int, 'return': None})
2513+
self.assertEqual(string_annos, {'b': 'undefined', 'return': 'None'})
2514+
25042515
# Check `init=False` attributes don't get into the annotations of the __init__ function
25052516
@dataclass
25062517
class C:

0 commit comments

Comments
 (0)