]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.15] gh-150641: Fix evaluating forward references in STRING format can 'leak' inter...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 3 Jul 2026 09:55:10 +0000 (11:55 +0200)
committerGitHub <noreply@github.com>
Fri, 3 Jul 2026 09:55:10 +0000 (11:55 +0200)
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 <fakeshadow1337@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2026-05-31-14-39-25.gh-issue-150641.LLIhd1.rst [new file with mode: 0644]

index 042604ed7c1a42399279068c22dd4bdb62b5aa3d..0c57c83491042dfcb859c25e00f5dcd68f10ae36 100644 (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):
 
index 6011b62cd26944fa232fe9857002a8bc2f2431ad..8b17cbbae2f6ec6283dcd2ac926a2ea680f14818 100644 (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
 
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 (file)
index 0000000..eba899c
--- /dev/null
@@ -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.