Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7561,6 +7561,18 @@ def test_with_module(self):
typing.evaluate_forward_ref(
fwdref_module.fw,)

def test_evaluate_forward_ref_string_format(self):
# Test evaluating forward references in STRING format
# does not 'leak' internal names
# See https://github.com/python/cpython/issues/150641

def f(arg: unknown | str | int | list[str] | tuple[int, ...]): ...

ref = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)['arg']
self.assertEqual(
typing.evaluate_forward_ref(ref, format=annotationlib.Format.STRING),
"unknown | str | int | list[str] | tuple[int, ...]",
)

class CollectionsAbcTests(BaseTestCase):

Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ def evaluate_forward_ref(

"""
if format == annotationlib.Format.STRING:
return forward_ref.__forward_arg__
return forward_ref.__resolved_str__
if forward_ref.__forward_arg__ in _recursive_guard:
return forward_ref

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug where :func:`typing.evaluate_forward_ref` with the ``STRING`` format
could leak internal names used by the annotation machinery.
Loading