]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45438: format of inspect.Signature with generic builtins (GH-29212)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 27 Oct 2021 22:00:18 +0000 (15:00 -0700)
committerGitHub <noreply@github.com>
Wed, 27 Oct 2021 22:00:18 +0000 (15:00 -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>
(cherry picked from commit d02ffd1b5c0fd8dec6dd2f7e3f2b0cfae48b7899)

Co-authored-by: Martin Rueckl <enigma@nbubu.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 976cbb1a234e446530bd8db5ecd2a83a6bd77f7d..64605c3cd3f6f381d8a8245ec158d0e26e7caf06 100644 (file)
@@ -1233,6 +1233,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 17c5d87f8ab45792c9c424be398655a38760e1b1..8a8844e912ad21ba0b6740422a44fcfccb067b0c 100644 (file)
@@ -3200,6 +3200,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.