Skip to content

Commit 1b89ad7

Browse files
miss-islingtonByteFlowing1337JelleZijlstra
authored
[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 f75028f) Co-authored-by: Ivy Xu <fakeshadow1337@gmail.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent d657028 commit 1b89ad7

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
@@ -7582,6 +7582,18 @@ def test_with_module(self):
75827582
typing.evaluate_forward_ref(
75837583
fwdref_module.fw,)
75847584

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

75867598
class CollectionsAbcTests(BaseTestCase):
75877599

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ def evaluate_forward_ref(
10321032
10331033
"""
10341034
if format == annotationlib.Format.STRING:
1035-
return forward_ref.__forward_arg__
1035+
return forward_ref.__resolved_str__
10361036
if forward_ref.__forward_arg__ in _recursive_guard:
10371037
return forward_ref
10381038

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)