From 80a3f0ecc335b9b750d71044a3daae5c1986017a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20R=C3=BCckl?= Date: Mon, 25 Oct 2021 15:44:39 +0200 Subject: [PATCH 1/2] bpo-45438: format of inspect.Signature with generic builtins Use types.GenericAlias in inspect.formatannotation to correctly add type arguments of builtin types to the string representation of Signatures. --- Lib/inspect.py | 2 ++ Lib/test/test_inspect.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/Lib/inspect.py b/Lib/inspect.py index a956acf310f85b..14a42fda56c65f 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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__ diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 516efac45e523f..7479eb326e6f5c 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -3236,6 +3236,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 From f976f10a9e8b1c03bc2e96b7a22e24a9e9a361e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20R=C3=BCckl?= Date: Wed, 27 Oct 2021 10:09:49 +0200 Subject: [PATCH 2/2] bpo-45438: add news entry --- .../NEWS.d/next/Library/2021-10-27-10-05-39.bpo-45438.Xz5lGU.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-10-27-10-05-39.bpo-45438.Xz5lGU.rst 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 00000000000000..cd6cfc1fcd4079 --- /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.