]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-137706: make typing._is_unpacked_typevartuple check for `True` instead...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 8 Oct 2025 04:23:47 +0000 (06:23 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Oct 2025 04:23:47 +0000 (21:23 -0700)
gh-137706: make typing._is_unpacked_typevartuple check for `True` instead of truthy (GH-137712)
(cherry picked from commit 7e652f402f84b412ec46cec42cb103f489a0087e)

Co-authored-by: David Ellis <ducksual@gmail.com>
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2025-09-05-21-10-24.gh-issue-137706.0EztiJ.rst [new file with mode: 0644]

index f19044bbcdeeaecf855ee60a926bcd1fc8cbca1a..b799be5ba7d8fd107ed2b5d778d5d2747eb94c0e 100644 (file)
@@ -9851,6 +9851,19 @@ class AnnotatedTests(BaseTestCase):
         self.assertIs(type(field_c2.__metadata__[0]), float)
         self.assertIs(type(field_c3.__metadata__[0]), bool)
 
+    def test_forwardref_partial_evaluation(self):
+        # Test that Annotated partially evaluates if it contains a ForwardRef
+        # See: https://github.com/python/cpython/issues/137706
+        def f(x: Annotated[undefined, '']): pass
+
+        ann = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)
+
+        # Test that the attributes are retrievable from the partially evaluated annotation
+        x_ann = ann['x']
+        self.assertIs(get_origin(x_ann), Annotated)
+        self.assertEqual(x_ann.__origin__, EqualToForwardRef('undefined', owner=f))
+        self.assertEqual(x_ann.__metadata__, ('',))
+
 
 class TypeAliasTests(BaseTestCase):
     def test_canonical_usage_with_variable_annotation(self):
index dbc8734418d9a9f7b5889ec6766a3b51bd24a18f..434cd0370e099ab70710e9dd925b08622c6f948f 100644 (file)
@@ -1024,8 +1024,10 @@ def evaluate_forward_ref(
 
 
 def _is_unpacked_typevartuple(x: Any) -> bool:
+    # Need to check 'is True' here
+    # See: https://github.com/python/cpython/issues/137706
     return ((not isinstance(x, type)) and
-            getattr(x, '__typing_is_unpacked_typevartuple__', False))
+            getattr(x, '__typing_is_unpacked_typevartuple__', False) is True)
 
 
 def _is_typevar_like(x: Any) -> bool:
diff --git a/Misc/NEWS.d/next/Library/2025-09-05-21-10-24.gh-issue-137706.0EztiJ.rst b/Misc/NEWS.d/next/Library/2025-09-05-21-10-24.gh-issue-137706.0EztiJ.rst
new file mode 100644 (file)
index 0000000..9eed50e
--- /dev/null
@@ -0,0 +1 @@
+Fix the partial evaluation of annotations that use ``typing.Annotated[T, x]`` where ``T`` is a forward reference.