mirror of https://github.com/python/cpython.git
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 d02ffd1b5c
)
Co-authored-by: Martin Rueckl <enigma@nbubu.de>
This commit is contained in:
parent
038f452308
commit
ce7a6afb79
|
@ -1357,6 +1357,8 @@ def getargvalues(frame):
|
|||
def formatannotation(annotation, base_module=None):
|
||||
if getattr(annotation, '__module__', None) == 'typing':
|
||||
return repr(annotation).replace('typing.', '')
|
||||
if isinstance(annotation, types.GenericAlias):
|
||||
return str(annotation)
|
||||
if isinstance(annotation, type):
|
||||
if annotation.__module__ in ('builtins', base_module):
|
||||
return annotation.__qualname__
|
||||
|
|
|
@ -3316,6 +3316,17 @@ def foo():
|
|||
pass
|
||||
self.assertEqual(str(inspect.signature(foo)), '()')
|
||||
|
||||
def foo(a: list[str]) -> tuple[str, float]:
|
||||
pass
|
||||
self.assertEqual(str(inspect.signature(foo)),
|
||||
'(a: list[str]) -> tuple[str, float]')
|
||||
|
||||
from typing import Tuple
|
||||
def foo(a: list[str]) -> Tuple[str, float]:
|
||||
pass
|
||||
self.assertEqual(str(inspect.signature(foo)),
|
||||
'(a: list[str]) -> Tuple[str, float]')
|
||||
|
||||
def test_signature_str_positional_only(self):
|
||||
P = inspect.Parameter
|
||||
S = inspect.Signature
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Fix typing.Signature string representation for generic builtin types.
|
Loading…
Reference in New Issue