https://github.com/python/cpython/commit/f2d433f01cdb8cd06f116e158ff03385b9b1559a
commit: f2d433f01cdb8cd06f116e158ff03385b9b1559a
branch: 3.14
author: Ivy Xu <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-07-06T18:07:12+01:00
summary:

[3.14] gh-150641: Fix evaluating forward references in STRING format can 'leak' 
internal names (GH-150648) (#153016)




(cherry picked from commit f75028f7ceebee4cbeb46bf040834e2005d57436)

Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-05-31-14-39-25.gh-issue-150641.LLIhd1.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 1832af632de4df..8786c1b4dfecda 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -7446,6 +7446,18 @@ def test_with_module(self):
         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 d9a7f7210bbcdd..9308e0820f55cb 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -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
index 00000000000000..eba899c96fcd26
--- /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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to