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 12:05:07 2016 -0700 @@ -591,11 +591,13 @@ >>> 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 +608,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 Doc/whatsnew/3.6.rst --- a/Doc/whatsnew/3.6.rst Tue May 31 20:17:58 2016 -0400 +++ b/Doc/whatsnew/3.6.rst Wed Jun 01 12:05:07 2016 -0700 @@ -281,6 +281,13 @@ :issue:`23848`.) +inspect +-------- + +The :meth:`~inspect.Signature.from_callable` method now supports the +*skip_bound_arg* argument. (Contributed by Ryan Petrello in :issue:`27172`.) + + os -- 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 12:05:07 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 12:05:07 2016 -0700 @@ -2865,6 +2865,19 @@ 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: + 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):