diff -r 20bd4c23cfe4 Doc/library/inspect.rst --- a/Doc/library/inspect.rst Tue May 31 20:17:58 2016 -0400 +++ b/Doc/library/inspect.rst Wed Jun 01 03:53:26 2016 -0700 @@ -591,7 +591,7 @@ >>> str(new_sig) "(a, b) -> 'new return anno'" - .. classmethod:: Signature.from_callable(obj, \*, follow_wrapped=True) + .. classmethod:: Signature.from_callable(obj, \*, follow_wrapped=True, skip_bound_arg=True) Return a :class:`Signature` (or its subclass) object for a given callable ``obj``. Pass ``follow_wrapped=False`` to get a signature of ``obj`` @@ -606,6 +606,11 @@ .. versionadded:: 3.5 + .. versionadded:: 3.6 + ``skip_bound_arg`` parameter. Pass ``False`` to get a signature of + ``callable`` that includes the bound argument (i.e., + ``obj.__self__``). + .. class:: Parameter(name, kind, \*, default=Parameter.empty, annotation=Parameter.empty) diff -r 20bd4c23cfe4 Lib/inspect.py --- a/Lib/inspect.py Tue May 31 20:17:58 2016 -0400 +++ b/Lib/inspect.py Wed Jun 01 03:53:26 2016 -0700 @@ -2711,10 +2711,11 @@ return _signature_from_builtin(cls, func) @classmethod - def from_callable(cls, obj, *, follow_wrapped=True): + def from_callable(cls, obj, *, follow_wrapped=True, skip_bound_arg=True): """Constructs Signature for the given callable object.""" return _signature_from_callable(obj, sigcls=cls, - follow_wrapper_chains=follow_wrapped) + follow_wrapper_chains=follow_wrapped, + skip_bound_arg=skip_bound_arg) @property def parameters(self): diff -r 20bd4c23cfe4 Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py Tue May 31 20:17:58 2016 -0400 +++ b/Lib/test/test_inspect.py Wed Jun 01 03:53:26 2016 -0700 @@ -2865,6 +2865,24 @@ foo_sig = MySignature.from_callable(foo) self.assertTrue(isinstance(foo_sig, MySignature)) + def test_signature_from_callable_python_obj_with_bound_arg(self): + class MyObj(object): + def foo(self, a, b, c): pass + inst = MyObj() + + foo_sig = inspect.Signature.from_callable(inst.foo) + self.assertEqual( + [param.name for param in foo_sig.parameters.values()], + ['a', 'b', 'c'] + ) + + with_self = inspect.Signature.from_callable(inst.foo, + skip_bound_arg=False) + self.assertEqual( + [param.name for param in with_self.parameters.values()], + ['self', 'a', 'b', 'c'] + ) + @unittest.skipIf(MISSING_C_DOCSTRINGS, "Signature information for builtins requires docstrings") def test_signature_from_callable_builtin_obj(self):