Skip to content

Commit bc6bd53

Browse files
gh-150641: Fix evaluating forward references in STRING format can 'leak' internal names in typing (#150648)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> (cherry picked from commit f75028f)
1 parent 4413f32 commit bc6bd53

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

Lib/test/test_typing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7446,6 +7446,18 @@ def test_with_module(self):
74467446
typing.evaluate_forward_ref(
74477447
fwdref_module.fw,)
74487448

7449+
def test_evaluate_forward_ref_string_format(self):
7450+
# Test evaluating forward references in STRING format
7451+
# does not 'leak' internal names
7452+
# See https://github.com/python/cpython/issues/150641
7453+
7454+
def f(arg: unknown | str | int | list[str] | tuple[int, ...]): ...
7455+
7456+
ref = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)['arg']
7457+
self.assertEqual(
7458+
typing.evaluate_forward_ref(ref, format=annotationlib.Format.STRING),
7459+
"unknown | str | int | list[str] | tuple[int, ...]",
7460+
)
74497461

74507462
class CollectionsAbcTests(BaseTestCase):
74517463

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ def evaluate_forward_ref(
991991
992992
"""
993993
if format == _lazy_annotationlib.Format.STRING:
994-
return forward_ref.__forward_arg__
994+
return forward_ref.__resolved_str__
995995
if forward_ref.__forward_arg__ in _recursive_guard:
996996
return forward_ref
997997

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug where :func:`typing.evaluate_forward_ref` with the ``STRING`` format
2+
could leak internal names used by the annotation machinery.

0 commit comments

Comments
 (0)