Skip to content
Open
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
17 changes: 17 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7561,6 +7561,23 @@ 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
from annotationlib import Format, get_annotations

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

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

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 evaluating forward references in STRING format can 'leak' internal names
in ``typing``.
Loading