]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.15] gh-151665: Fix inspect.signature() on type alias and type parameter evaluators...
authorTimofei <128279579+deadlovelll@users.noreply.github.com>
Sun, 21 Jun 2026 22:08:37 +0000 (01:08 +0300)
committerGitHub <noreply@github.com>
Sun, 21 Jun 2026 22:08:37 +0000 (15:08 -0700)
Lib/inspect.py
Lib/test/test_type_params.py
Misc/NEWS.d/next/Library/2026-06-20-14-47-55.gh-issue-151665.82fmzx.rst [new file with mode: 0644]

index dc5a6e3be883bb061607b28fe17f9f543c2c0e0b..0dbd596db160236173a8f3920605c1e37017288c 100644 (file)
@@ -2720,6 +2720,10 @@ class Parameter:
                 raise ValueError(msg)
             self._kind = _POSITIONAL_ONLY
             name = 'implicit{}'.format(name[1:])
+        elif name == '.format':
+            # gh-151665: Hidden parameter of compiler-generated annotation and type
+            # alias/typevar evaluators. Show it as "format".
+            name = 'format'
 
         # It's possible for C functions to have a positional-only parameter
         # where the name is a keyword, so for compatibility we'll allow it.
index 84c1b9541367363685f25d6131dddb491ef853eb..27660379ec43a371339d7cec87a4a49c98bec687 100644 (file)
@@ -1,4 +1,5 @@
 import annotationlib
+import inspect
 import textwrap
 import types
 import unittest
@@ -1446,6 +1447,30 @@ class TestEvaluateFunctions(unittest.TestCase):
                 self.assertIs(annotationlib.call_evaluate_function(case, annotationlib.Format.FORWARDREF), int)
                 self.assertEqual(annotationlib.call_evaluate_function(case, annotationlib.Format.STRING), 'int')
 
+    def test_signature(self):
+        # gh-151665: the ".format" parameter of compiler-generated evaluators
+        # used to break inspect.signature(). It should show up as "format".
+        type Alias = int
+        def f[T: int = int, **P = int, *Ts = int](): pass
+        T, P, Ts = f.__type_params__
+        def g[T: (int, str)](): pass
+        T3, = g.__type_params__
+        cases = [
+            Alias.evaluate_value,
+            T.evaluate_bound,
+            T.evaluate_default,
+            P.evaluate_default,
+            Ts.evaluate_default,
+            T3.evaluate_constraints,
+        ]
+        for case in cases:
+            with self.subTest(case=case):
+                sig = inspect.signature(case)
+                self.assertEqual(str(sig), '(format=1, /)')
+                param, = sig.parameters.values()
+                self.assertEqual(param.name, 'format')
+                self.assertIs(param.kind, inspect.Parameter.POSITIONAL_ONLY)
+
     def test_constraints(self):
         def f[T: (int, str)](): pass
         T, = f.__type_params__
diff --git a/Misc/NEWS.d/next/Library/2026-06-20-14-47-55.gh-issue-151665.82fmzx.rst b/Misc/NEWS.d/next/Library/2026-06-20-14-47-55.gh-issue-151665.82fmzx.rst
new file mode 100644 (file)
index 0000000..d08a122
--- /dev/null
@@ -0,0 +1,2 @@
+:func:`inspect.signature` now works on the lazy evaluators of type aliases
+and type parameters instead of raising :exc:`ValueError`.