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 10:18:10 2016 -0700 @@ -591,11 +591,14 @@ >>> 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`` - without unwrapping its ``__wrapped__`` chain. + without unwrapping its ``__wrapped__`` chain. Pass + ``skip_bound_arg=False`` to get a signature of ``obj`` that includes the + bound argument (i.e., ``obj.__self__``). + This method simplifies subclassing of :class:`Signature`:: @@ -606,6 +609,8 @@ .. versionadded:: 3.5 + .. versionchanged:: 3.6 + The ``skip_bound_arg`` argument was added. .. 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 10:18:10 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 10:18:10 2016 -0700 @@ -2865,6 +2865,18 @@ 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(list(foo_sig.parameters), ['a', 'b', 'c']) + + with_self = inspect.Signature.from_callable(inst.foo, + skip_bound_arg=False) + self.assertEqual(list(with_self.parameters), ['self', 'a', 'b', 'c']) + @unittest.skipIf(MISSING_C_DOCSTRINGS, "Signature information for builtins requires docstrings") def test_signature_from_callable_builtin_obj(self):