inspect.signature: Improve repr of Signature and Parameter. Closes #20378
This commit is contained in:
parent
722e2489bb
commit
374375dd26
|
@ -2192,8 +2192,8 @@ class Parameter:
|
|||
return formatted
|
||||
|
||||
def __repr__(self):
|
||||
return '<{} at {:#x} {!r}>'.format(self.__class__.__name__,
|
||||
id(self), self.name)
|
||||
return '<{} at {:#x} "{}">'.format(self.__class__.__name__,
|
||||
id(self), self)
|
||||
|
||||
def __eq__(self, other):
|
||||
# NB: We deliberately do not compare '_partial_kwarg' attributes
|
||||
|
@ -2698,6 +2698,10 @@ class Signature:
|
|||
def __setstate__(self, state):
|
||||
self._return_annotation = state['_return_annotation']
|
||||
|
||||
def __repr__(self):
|
||||
return '<{} at {:#x} "{}">'.format(self.__class__.__name__,
|
||||
id(self), self)
|
||||
|
||||
def __str__(self):
|
||||
result = []
|
||||
render_pos_only_separator = False
|
||||
|
|
|
@ -1667,6 +1667,9 @@ class TestSignatureObject(unittest.TestCase):
|
|||
with self.assertRaisesRegex(ValueError, 'follows default argument'):
|
||||
S((pkd, pk))
|
||||
|
||||
self.assertTrue(repr(sig).startswith('<Signature'))
|
||||
self.assertTrue('"(po, pk' in repr(sig))
|
||||
|
||||
def test_signature_object_pickle(self):
|
||||
def foo(a, b, *, c:1={}, **kw) -> {42:'ham'}: pass
|
||||
foo_partial = functools.partial(foo, a=1)
|
||||
|
@ -2575,6 +2578,7 @@ class TestParameterObject(unittest.TestCase):
|
|||
p.replace(kind=inspect.Parameter.VAR_POSITIONAL)
|
||||
|
||||
self.assertTrue(repr(p).startswith('<Parameter'))
|
||||
self.assertTrue('"a=42"' in repr(p))
|
||||
|
||||
def test_signature_parameter_equality(self):
|
||||
P = inspect.Parameter
|
||||
|
|
Loading…
Reference in New Issue