bpo-45438: format of inspect.Signature with generic builtins (#29212) · python/cpython@d02ffd1 (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

Commit d02ffd1

martinitusMRuecklCC

and

authored

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

File tree

3 files changed

lines changed

3 files changed

lines changed

Lines changed: 2 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -1325,6 +1325,8 @@ def getargvalues(frame):
1325 1325 def formatannotation(annotation, base_module=None):
1326 1326 if getattr(annotation, '__module__', None) == 'typing':
1327 1327 return repr(annotation).replace('typing.', '')
1328 +if isinstance(annotation, types.GenericAlias):
1329 +return str(annotation)
1328 1330 if isinstance(annotation, type):
1329 1331 if annotation.__module__ in ('builtins', base_module):
1330 1332 return annotation.__qualname__

Lines changed: 11 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -3236,6 +3236,17 @@ def foo():
3236 3236 pass
3237 3237 self.assertEqual(str(inspect.signature(foo)), '()')
3238 3238
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 +
3239 3250 def test_signature_str_positional_only(self):
3240 3251 P = inspect.Parameter
3241 3252 S = inspect.Signature

Lines changed: 1 addition & 0 deletions

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 +Fix typing.Signature string representation for generic builtin types.