]> granicus.if.org Git - python/commitdiff
Issue 22547: Implement informative __repr__ for inspect.BoundArguments
authorYury Selivanov <yselivanov@sprymix.com>
Thu, 14 May 2015 22:47:17 +0000 (18:47 -0400)
committerYury Selivanov <yselivanov@sprymix.com>
Thu, 14 May 2015 22:47:17 +0000 (18:47 -0400)
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index ef2407cfb3eb2c4a7c04214f41709b5fa7b1f9be..9290b4b55f183a58f657fc7ff223d946103bad2a 100644 (file)
@@ -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.
index bbb7afa7bad0dc316832e15ed33da7270e96ed54..6f813be89573ab8a9f6bffe7c7e10da90abe1ed6 100644 (file)
@@ -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'<BoundArguments at 0x[a-fA-F0-9]+ \(a=20,.*\}\}\)>')
+
 
 class TestSignaturePrivateHelpers(unittest.TestCase):
     def test_signature_get_bound_param(self):
index b181f7682320f6969f4c2b2696a7e7331b563949..8414ea833251894fed739b145f7b6cdca8c3d672 100644 (file)
--- 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
 -----