Skip to content

Commit ce7a6af

Browse files
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@codecentric.de> (cherry picked from commit d02ffd1) Co-authored-by: Martin Rueckl <enigma@nbubu.de>
1 parent 038f452 commit ce7a6af

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
@@ -1357,6 +1357,8 @@ def getargvalues(frame):
13571357
def formatannotation(annotation, base_module=None):
13581358
if getattr(annotation, '__module__', None) == 'typing':
13591359
return repr(annotation).replace('typing.', '')
1360+
if isinstance(annotation, types.GenericAlias):
1361+
return str(annotation)
13601362
if isinstance(annotation, type):
13611363
if annotation.__module__ in ('builtins', base_module):
13621364
return annotation.__qualname__

Lib/test/test_inspect.py

+11
Original file line numberDiff line numberDiff line change
@@ -3316,6 +3316,17 @@ def foo():
33163316
pass
33173317
self.assertEqual(str(inspect.signature(foo)), '()')
33183318

3319+
def foo(a: list[str]) -> tuple[str, float]:
3320+
pass
3321+
self.assertEqual(str(inspect.signature(foo)),
3322+
'(a: list[str]) -> tuple[str, float]')
3323+
3324+
from typing import Tuple
3325+
def foo(a: list[str]) -> Tuple[str, float]:
3326+
pass
3327+
self.assertEqual(str(inspect.signature(foo)),
3328+
'(a: list[str]) -> Tuple[str, float]')
3329+
33193330
def test_signature_str_positional_only(self):
33203331
P = inspect.Parameter
33213332
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)