Skip to content

Commit f75028f

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>
1 parent 8c35afa commit f75028f

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
@@ -7587,6 +7587,18 @@ def test_with_module(self):
75877587
typing.evaluate_forward_ref(
75887588
fwdref_module.fw,)
75897589

7590+
def test_evaluate_forward_ref_string_format(self):
7591+
# Test evaluating forward references in STRING format
7592+
# does not 'leak' internal names
7593+
# See https://github.com/python/cpython/issues/150641
7594+
7595+
def f(arg: unknown | str | int | list[str] | tuple[int, ...]): ...
7596+
7597+
ref = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)['arg']
7598+
self.assertEqual(
7599+
typing.evaluate_forward_ref(ref, format=annotationlib.Format.STRING),
7600+
"unknown | str | int | list[str] | tuple[int, ...]",
7601+
)
75907602

75917603
class CollectionsAbcTests(BaseTestCase):
75927604

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ def evaluate_forward_ref(
10191019
10201020
"""
10211021
if format == annotationlib.Format.STRING:
1022-
return forward_ref.__forward_arg__
1022+
return forward_ref.__resolved_str__
10231023
if forward_ref.__forward_arg__ in _recursive_guard:
10241024
return forward_ref
10251025

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)