New submission from Petr Viktorin:

When obtaining the signature of a bound method, inspect.signature, by default, 
omits the "self" argument to the method, since it is already specified in the 
bound method.  However, if you create a wrapper around a bound method with 
functools.update_wrapper() or @functools.wraps, calling inspect.signature on 
the wrapper will return a signature which includes the "self" argument.

Reproducer:

    import inspect
    import functools

    class Foo(object):
        def bar(self, testarg):
            pass

    f = Foo()

    @functools.wraps(f.bar)
    def baz(*args):
        f.bar(*args)


    assert inspect.signature(baz) == inspect.signature(f.bar)

The program will fail with an assertion error. Examining inspect.signature(baz) 
shows:

    >>> print(inspect.signature(baz))
    (self, testarg)
    >>> print(inspect.signature(f.bar))
    (testarg)

Looking at the code in inspect.py:

The handling of bound methods appears at the top of 
inspect._signature_internal().  Since baz is not itself a bound method, it 
doesn't trigger this case.  Instead inspect.unwrap is called, returning f.bar.

inspect._signature_is_functionlike(f.bar) returns True, causing 
Signature.from_function to be called.  Unlike the direct bound method case, 
this includes the bound method's "self" argument.

----------
messages: 244178
nosy: encukou
priority: normal
severity: normal
status: open
title: inspect.signature includes bound argument for wrappers around bound 
methods
versions: Python 3.4, Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to