]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue 24298: Fix signature() to properly unwrap wrappers around bound methods
authorYury Selivanov <yselivanov@sprymix.com>
Thu, 28 May 2015 01:56:53 +0000 (21:56 -0400)
committerYury Selivanov <yselivanov@sprymix.com>
Thu, 28 May 2015 01:56:53 +0000 (21:56 -0400)
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index a91f749d520d0bf2d4882a27ae19c60f4a58b7a3..49e66ce078d4807eebdfa822d1aaccafa65e3623 100644 (file)
@@ -1911,6 +1911,14 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
     # Was this function wrapped by a decorator?
     if follow_wrapper_chains:
         obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
+        if isinstance(obj, types.MethodType):
+            # If the unwrapped object is a *method*, we might want to
+            # skip its first parameter (self).
+            # See test_signature_wrapped_bound_method for details.
+            return _signature_internal(
+                obj,
+                follow_wrapper_chains=follow_wrapper_chains,
+                skip_bound_arg=skip_bound_arg)
 
     try:
         sig = obj.__signature__
index 8d92f8230e39d4fa5bc943a7333bdd896252cde5..58a6cd2c567ca6ef78daad3fe696746beaff750b 100644 (file)
@@ -1939,6 +1939,19 @@ class TestSignatureObject(unittest.TestCase):
         with self.assertRaisesRegex(ValueError, 'invalid method signature'):
             self.signature(Test())
 
+    def test_signature_wrapped_bound_method(self):
+        # Issue 24298
+        class Test:
+            def m1(self, arg1, arg2=1) -> int:
+                pass
+        @functools.wraps(Test().m1)
+        def m1d(*args, **kwargs):
+            pass
+        self.assertEqual(self.signature(m1d),
+                         ((('arg1', ..., ..., "positional_or_keyword"),
+                           ('arg2', 1, ..., "positional_or_keyword")),
+                          int))
+
     def test_signature_on_classmethod(self):
         class Test:
             @classmethod
index a58ddf232b0fd3cb14b62fda3dbce04c6d6b5550..76580093f3034c83e09eee22b72cb70af26a2ad3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -274,6 +274,9 @@ Library
 - Issue #23898: Fix inspect.classify_class_attrs() to support attributes
   with overloaded __eq__ and __bool__.  Patch by Mike Bayer.
 
+- Issue #24298: Fix inspect.signature() to correctly unwrap wrappers
+  around bound methods.
+
 IDLE
 ----