From: Yury Selivanov Date: Thu, 14 May 2015 22:47:17 +0000 (-0400) Subject: Issue 22547: Implement informative __repr__ for inspect.BoundArguments X-Git-Tag: v3.5.0b1~132 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3f6538fed0e2e8905805f3d30937cd41d26e032b;p=python Issue 22547: Implement informative __repr__ for inspect.BoundArguments --- diff --git a/Lib/inspect.py b/Lib/inspect.py index ef2407cfb3..9290b4b55f 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2460,6 +2460,13 @@ class BoundArguments: 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 --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index bbb7afa7ba..6f813be895 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -3149,6 +3149,13 @@ class TestBoundArguments(unittest.TestCase): 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 --git a/Misc/NEWS b/Misc/NEWS index b181f76823..8414ea8332 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -125,6 +125,9 @@ Library - 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 -----