[3.15] gh-150641: Fix evaluating forward references in STRING format can 'leak' internal names in typing (GH-150648) (#152935)

gh-150641: Fix evaluating forward references in STRING format can 'leak' internal names in `typing` (GH-150648)
(cherry picked from commit f75028f7ce)

Co-authored-by: Ivy Xu <fakeshadow1337@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Miss Islington (bot)
2026-07-03 11:55:10 +02:00
committed by GitHub
parent d657028a6e
commit 1b89ad7e00
3 changed files with 15 additions and 1 deletions
+12
View File
@@ -7582,6 +7582,18 @@ class EvaluateForwardRefTests(BaseTestCase):
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):
+1 -1
View File
@@ -1032,7 +1032,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
@@ -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.