]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-150641: Fix evaluating forward references in STRING format can 'leak' internal...
authorIvy Xu <fakeshadow1337@gmail.com>
Fri, 3 Jul 2026 08:42:06 +0000 (16:42 +0800)
committerGitHub <noreply@github.com>
Fri, 3 Jul 2026 08:42:06 +0000 (10:42 +0200)
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 ed07503cd63f12b83ec8dffabc3837a23d5c8f83..17574c7f03d8e1e5ba1070bad4ccc120e69111ea 100644 (file)
@@ -7587,6 +7587,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 1579f492003f748bb5cd7aec1e02235153e6f4d3..933336ff4cf37e2a618cda8e32e60caf69eb2a05 100644 (file)
@@ -1019,7 +1019,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.