Skip to content

Commit 21150c6

Browse files
miss-islingtonmartinitus
andauthoredOct 27, 2021
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 30c1f18 commit 21150c6

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
@@ -1233,6 +1233,8 @@ def getargvalues(frame):
12331233
def formatannotation(annotation, base_module=None):
12341234
if getattr(annotation, '__module__', None) == 'typing':
12351235
return repr(annotation).replace('typing.', '')
1236+
if isinstance(annotation, types.GenericAlias):
1237+
return str(annotation)
12361238
if isinstance(annotation, type):
12371239
if annotation.__module__ in ('builtins', base_module):
12381240
return annotation.__qualname__

‎Lib/test/test_inspect.py

+11
Original file line numberDiff line numberDiff line change
@@ -3200,6 +3200,17 @@ def foo():
32003200
pass
32013201
self.assertEqual(str(inspect.signature(foo)), '()')
32023202

3203+
def foo(a: list[str]) -> tuple[str, float]:
3204+
pass
3205+
self.assertEqual(str(inspect.signature(foo)),
3206+
'(a: list[str]) -> tuple[str, float]')
3207+
3208+
from typing import Tuple
3209+
def foo(a: list[str]) -> Tuple[str, float]:
3210+
pass
3211+
self.assertEqual(str(inspect.signature(foo)),
3212+
'(a: list[str]) -> Tuple[str, float]')
3213+
32033214
def test_signature_str_positional_only(self):
32043215
P = inspect.Parameter
32053216
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)
Please sign in to comment.