changeset: 96059:a444464a2e87 user: Yury Selivanov date: Thu May 14 18:47:17 2015 -0400 files: Lib/inspect.py Lib/test/test_inspect.py Misc/NEWS description: Issue 22547: Implement informative __repr__ for inspect.BoundArguments diff -r 1cdcce187c43 -r a444464a2e87 Lib/inspect.py --- a/Lib/inspect.py Thu May 14 18:33:14 2015 -0400 +++ b/Lib/inspect.py Thu May 14 18:47:17 2015 -0400 @@ -2460,6 +2460,13 @@ def __getstate__(self): return {'_signature': self._signature, 'arguments': self.arguments} + def __repr__(self): + args = [] + for arg, value in self.arguments.items(): + args.append('{}={!r}'.format(arg, value)) + return '<{} at {:#x} ({})>'.format(self.__class__.__name__, + id(self), ', '.join(args)) + class Signature: """A Signature object represents the overall signature of a function. diff -r 1cdcce187c43 -r a444464a2e87 Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py Thu May 14 18:33:14 2015 -0400 +++ b/Lib/test/test_inspect.py Thu May 14 18:47:17 2015 -0400 @@ -3149,6 +3149,13 @@ ba_pickled = pickle.loads(pickle.dumps(ba, ver)) self.assertEqual(ba, ba_pickled) + def test_signature_bound_arguments_repr(self): + def foo(a, b, *, c:1={}, **kw) -> {42:'ham'}: pass + sig = inspect.signature(foo) + ba = sig.bind(20, 30, z={}) + self.assertRegex(repr(ba), + r'') + class TestSignaturePrivateHelpers(unittest.TestCase): def test_signature_get_bound_param(self): diff -r 1cdcce187c43 -r a444464a2e87 Misc/NEWS --- a/Misc/NEWS Thu May 14 18:33:14 2015 -0400 +++ b/Misc/NEWS Thu May 14 18:47:17 2015 -0400 @@ -125,6 +125,9 @@ - Issue 24184: Add AsyncIterator and AsyncIterable ABCs to collections.abc. Contributed by Yury Selivanov. +- Issue 22547: Implement informative __repr__ for inspect.BoundArguments. + Contributed by Yury Selivanov. + Tests -----