]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45438: format of inspect.Signature with generic builtins (#29212)
authorMartin Rueckl <enigma@nbubu.de>
Wed, 27 Oct 2021 21:36:41 +0000 (23:36 +0200)
committerGitHub <noreply@github.com>
Wed, 27 Oct 2021 21:36:41 +0000 (14:36 -0700)
Use types.GenericAlias in inspect.formatannotation to correctly add
type arguments of builtin types to the string representation of
Signatures.

Co-authored-by: Martin Rückl <martin.rueckl@codecentric.de>
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS.d/next/Library/2021-10-27-10-05-39.bpo-45438.Xz5lGU.rst [new file with mode: 0644]

index a956acf310f85bbfa2119bf62140b02554bdf707..14a42fda56c65f3cd2cfcc20842c5ccd6a28168f 100644 (file)
@@ -1325,6 +1325,8 @@ def getargvalues(frame):
 def formatannotation(annotation, base_module=None):
     if getattr(annotation, '__module__', None) == 'typing':
         return repr(annotation).replace('typing.', '')
+    if isinstance(annotation, types.GenericAlias):
+        return str(annotation)
     if isinstance(annotation, type):
         if annotation.__module__ in ('builtins', base_module):
             return annotation.__qualname__
index 516efac45e523f99a66eaaeed39ea38eef20eaed..7479eb326e6f5c875f865179b5ec10176ca11c1f 100644 (file)
@@ -3236,6 +3236,17 @@ class TestSignatureObject(unittest.TestCase):
             pass
         self.assertEqual(str(inspect.signature(foo)), '()')
 
+        def foo(a: list[str]) -> tuple[str, float]:
+            pass
+        self.assertEqual(str(inspect.signature(foo)),
+                         '(a: list[str]) -> tuple[str, float]')
+
+        from typing import Tuple
+        def foo(a: list[str]) -> Tuple[str, float]:
+            pass
+        self.assertEqual(str(inspect.signature(foo)),
+                         '(a: list[str]) -> Tuple[str, float]')
+
     def test_signature_str_positional_only(self):
         P = inspect.Parameter
         S = inspect.Signature
diff --git a/Misc/NEWS.d/next/Library/2021-10-27-10-05-39.bpo-45438.Xz5lGU.rst b/Misc/NEWS.d/next/Library/2021-10-27-10-05-39.bpo-45438.Xz5lGU.rst
new file mode 100644 (file)
index 0000000..cd6cfc1
--- /dev/null
@@ -0,0 +1 @@
+Fix typing.Signature string representation for generic builtin types.