[Python-checkins] bpo-45438: format of inspect.Signature with generic builtins (GH-29212)
miss-islington
webhook-mailer at python.org
Wed Oct 27 18:00:28 EDT 2021
https://github.com/python/cpython/commit/21150c6fa330f80747d698e4b883c7b4801a25bd
commit: 21150c6fa330f80747d698e4b883c7b4801a25bd
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-10-27T15:00:18-07:00
summary:
bpo-45438: format of inspect.Signature with generic builtins (GH-29212)
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 at codecentric.de>
(cherry picked from commit d02ffd1b5c0fd8dec6dd2f7e3f2b0cfae48b7899)
Co-authored-by: Martin Rueckl <enigma at nbubu.de>
files:
A Misc/NEWS.d/next/Library/2021-10-27-10-05-39.bpo-45438.Xz5lGU.rst
M Lib/inspect.py
M Lib/test/test_inspect.py
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 976cbb1a234e4..64605c3cd3f6f 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -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__
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 17c5d87f8ab45..8a8844e912ad2 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -3200,6 +3200,17 @@ def foo():
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
index 0000000000000..cd6cfc1fcd407
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-10-27-10-05-39.bpo-45438.Xz5lGU.rst
@@ -0,0 +1 @@
+Fix typing.Signature string representation for generic builtin types.
More information about the Python-checkins
mailing list