inspect.signature: Make sure that if a callable object has '_patialmethod'
authorYury Selivanov <yselivanov@sprymix.com>
Wed, 29 Jan 2014 17:18:59 +0000 (12:18 -0500)
committerYury Selivanov <yselivanov@sprymix.com>
Wed, 29 Jan 2014 17:18:59 +0000 (12:18 -0500)
attribute, that attribute is an instance of 'functools.partialmethod'.

Lib/inspect.py
Lib/test/test_inspect.py

index fbea3dff550fd262a57655b7da02347d53bc7ddb..3ad4a825dc685166acaab1248a401cc50d5368d7 100644 (file)
@@ -1651,20 +1651,21 @@ def signature(obj):
     except AttributeError:
         pass
     else:
-        # Unbound partialmethod (see functools.partialmethod)
-        # This means, that we need to calculate the signature
-        # as if it's a regular partial object, but taking into
-        # account that the first positional argument
-        # (usually `self`, or `cls`) will not be passed
-        # automatically (as for boundmethods)
+        if isinstance(partialmethod, functools.partialmethod):
+            # Unbound partialmethod (see functools.partialmethod)
+            # This means, that we need to calculate the signature
+            # as if it's a regular partial object, but taking into
+            # account that the first positional argument
+            # (usually `self`, or `cls`) will not be passed
+            # automatically (as for boundmethods)
 
-        wrapped_sig = signature(partialmethod.func)
-        sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
+            wrapped_sig = signature(partialmethod.func)
+            sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
 
-        first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
-        new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
+            first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
+            new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
 
-        return sig.replace(parameters=new_params)
+            return sig.replace(parameters=new_params)
 
     if _signature_is_builtin(obj):
         return Signature.from_builtin(obj)
index 19ccbcba65c6454e2a6112e6a84c15d99a7f3fed..f06b5b4ff47b706133b007621cb138aff23896af 100644 (file)
@@ -1983,6 +1983,11 @@ class TestSignatureObject(unittest.TestCase):
                            ('c', 1, ..., 'keyword_only')),
                           'spam'))
 
+    def test_signature_on_fake_partialmethod(self):
+        def foo(a): pass
+        foo._partialmethod = 'spam'
+        self.assertEqual(str(inspect.signature(foo)), '(a)')
+
     def test_signature_on_decorated(self):
         import functools