Skip to content

Commit d02ffd1

Browse files
bpo-45438: format of inspect.Signature with generic builtins (#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@codecentric.de>
1 parent 10bbd41 commit d02ffd1

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/inspect.py

+2
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,8 @@ def getargvalues(frame):
13251325
def formatannotation(annotation, base_module=None):
13261326
if getattr(annotation, '__module__', None) == 'typing':
13271327
return repr(annotation).replace('typing.', '')
1328+
if isinstance(annotation, types.GenericAlias):
1329+
return str(annotation)
13281330
if isinstance(annotation, type):
13291331
if annotation.__module__ in ('builtins', base_module):
13301332
return annotation.__qualname__

Lib/test/test_inspect.py

+11
Original file line numberDiff line numberDiff line change
@@ -3236,6 +3236,17 @@ def foo():
32363236
pass
32373237
self.assertEqual(str(inspect.signature(foo)), '()')
32383238

3239+
def foo(a: list[str]) -> tuple[str, float]:
3240+
pass
3241+
self.assertEqual(str(inspect.signature(foo)),
3242+
'(a: list[str]) -> tuple[str, float]')
3243+
3244+
from typing import Tuple
3245+
def foo(a: list[str]) -> Tuple[str, float]:
3246+
pass
3247+
self.assertEqual(str(inspect.signature(foo)),
3248+
'(a: list[str]) -> Tuple[str, float]')
3249+
32393250
def test_signature_str_positional_only(self):
32403251
P = inspect.Parameter
32413252
S = inspect.Signature
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix typing.Signature string representation for generic builtin types.

0 commit comments

Comments
 (0)