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

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

Commit ce7a6af

miss-islingtonmartinitus

and

authored

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

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

Lines changed: 11 additions & 0 deletions

Original file line number Diff line number Diff line change
@@ -3316,6 +3316,17 @@ def foo():
3316 3316 pass
3317 3317 self.assertEqual(str(inspect.signature(foo)), '()')
3318 3318
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 +
3319 3330 def test_signature_str_positional_only(self):
3320 3331 P = inspect.Parameter
3321 3332 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.