From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Fri, 3 Jul 2026 09:55:10 +0000 (+0200) Subject: [3.15] gh-150641: Fix evaluating forward references in STRING format can 'leak' inter... X-Git-Tag: v3.15.0b4~142 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1b89ad7e009c00da78aab3eff73620fafb5b032e;p=thirdparty%2FPython%2Fcpython.git [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 f75028f7ceebee4cbeb46bf040834e2005d57436) Co-authored-by: Ivy Xu Co-authored-by: Jelle Zijlstra --- diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 042604ed7c1a..0c57c8349104 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -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): diff --git a/Lib/typing.py b/Lib/typing.py index 6011b62cd269..8b17cbbae2f6 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -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 diff --git a/Misc/NEWS.d/next/Library/2026-05-31-14-39-25.gh-issue-150641.LLIhd1.rst b/Misc/NEWS.d/next/Library/2026-05-31-14-39-25.gh-issue-150641.LLIhd1.rst new file mode 100644 index 000000000000..eba899c96fcd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-31-14-39-25.gh-issue-150641.LLIhd1.rst @@ -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.