From 374375dd26118ccb7027f66d66ab9e80c82f4103 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Thu, 27 Mar 2014 12:41:53 -0400 Subject: [PATCH] inspect.signature: Improve repr of Signature and Parameter. Closes #20378 --- Lib/inspect.py | 8 ++++++-- Lib/test/test_inspect.py | 4 ++++ Misc/NEWS | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index acb80714ff..05d67fce54 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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 diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 5bdd327903..95b88779f9 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -1667,6 +1667,9 @@ class TestSignatureObject(unittest.TestCase): with self.assertRaisesRegex(ValueError, 'follows default argument'): S((pkd, pk)) + self.assertTrue(repr(sig).startswith(' {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('