]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-150641: Fix evaluating forward references in STRING format can 'leak' inter...
authorIvy Xu <fakeshadow1337@gmail.com>
Mon, 6 Jul 2026 17:07:12 +0000 (01:07 +0800)
committerGitHub <noreply@github.com>
Mon, 6 Jul 2026 17:07:12 +0000 (18:07 +0100)
(cherry picked from commit f75028f7ceebee4cbeb46bf040834e2005d57436)

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 1832af632de4dffc5145be04d342d5533839ba71..8786c1b4dfecda674db73876b5a0baf06a162ef6 100644 (file)
@@ -7446,6 +7446,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 d9a7f7210bbcdd22cd7587ab2f38f0d756c8a478..9308e0820f55cb69a47beddd9fa14dabbf5dc3ac 100644 (file)
@@ -991,7 +991,7 @@ def evaluate_forward_ref(
 
     """
     if format == _lazy_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.