From ce7a6afb797d2ffde45e9e902516b8437c8f9e31 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 27 Oct 2021 14:57:07 -0700 Subject: bpo-45438: format of inspect.Signature with generic builtins (GH-29212) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 (cherry picked from commit d02ffd1b5c0fd8dec6dd2f7e3f2b0cfae48b7899) Co-authored-by: Martin Rueckl --- Lib/inspect.py | 2 ++ Lib/test/test_inspect.py | 11 +++++++++++ .../next/Library/2021-10-27-10-05-39.bpo-45438.Xz5lGU.rst | 1 + 3 files changed, 14 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-10-27-10-05-39.bpo-45438.Xz5lGU.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index 2374e59..531b891 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1357,6 +1357,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 f0dc6bb..4164634 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -3316,6 +3316,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 index 0000000..cd6cfc1 --- /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. -- cgit v0.12