[Python-checkins] cpython (merge 3.4 -> 3.5): Issue 24298: Fix signature() to properly unwrap wrappers around bound methods

yury.selivanov python-checkins at python.org
Thu May 28 04:01:29 CEST 2015


https://hg.python.org/cpython/rev/d7e392c5c53a
changeset:   96324:d7e392c5c53a
branch:      3.5
parent:      96320:3d558ecd9936
parent:      96323:42819b176d63
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Wed May 27 21:59:03 2015 -0400
summary:
  Issue 24298: Fix signature() to properly unwrap wrappers around bound methods

files:
  Lib/inspect.py           |   9 +++++++++
  Lib/test/test_inspect.py |  13 +++++++++++++
  Misc/NEWS                |   3 +++
  3 files changed, 25 insertions(+), 0 deletions(-)


diff --git a/Lib/inspect.py b/Lib/inspect.py
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2130,6 +2130,15 @@
     # 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_from_callable(
+                obj,
+                follow_wrapper_chains=follow_wrapper_chains,
+                skip_bound_arg=skip_bound_arg,
+                sigcls=sigcls)
 
     try:
         sig = obj.__signature__
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -2086,6 +2086,19 @@
         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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -248,6 +248,9 @@
 - 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
 ----
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list