Skip to content

Commit 12d7a45

Browse files
committed
Add tests for replacement of extra names in repr and string evaluation
1 parent 2a07ff9 commit 12d7a45

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Lib/test/test_annotationlib.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,11 @@ def test_forward_repr(self):
19611961
"typing.List[ForwardRef('int', owner='class')]",
19621962
)
19631963

1964+
def test_forward_repr_extra_names(self):
1965+
fr = ForwardRef("__annotationlib_name_1__")
1966+
fr.__extra_names__ = {"__annotationlib_name_1__": list[str]}
1967+
self.assertEqual(repr(fr), "ForwardRef('list[str]')")
1968+
19641969
def test_forward_recursion_actually(self):
19651970
def namespace1():
19661971
a = ForwardRef("A")
@@ -2037,6 +2042,20 @@ def test_evaluate_string_format(self):
20372042
fr = ForwardRef("set[Any]")
20382043
self.assertEqual(fr.evaluate(format=Format.STRING), "set[Any]")
20392044

2045+
def test_evaluate_string_format_extra_names(self):
2046+
# Test that internal extra_names are replaced when evaluating as strings
2047+
2048+
# As identifier
2049+
fr = ForwardRef("__annotationlib_name_1__")
2050+
fr.__extra_names__ = {"__annotationlib_name_1__": str}
2051+
self.assertEqual(fr.evaluate(format=Format.STRING), "str")
2052+
2053+
# Via AST visitor
2054+
def f(a: ref | str): ...
2055+
2056+
fr = get_annotations(f, format=Format.FORWARDREF)['a']
2057+
self.assertEqual(fr.evaluate(format=Format.STRING), "ref | str")
2058+
20402059
def test_evaluate_forwardref_format(self):
20412060
fr = ForwardRef("undef")
20422061
evaluated = fr.evaluate(format=Format.FORWARDREF)

0 commit comments

Comments
 (0)