Bug 1201990 - inspect.signature includes bound argument for wrappers around bound methods
Summary: inspect.signature includes bound argument for wrappers around bound methods
Keywords:
Status: CLOSED EOL
Alias: None
Product: Fedora
Classification: Fedora
Component: python3
Version: 22
Hardware: All
OS: All
unspecified
medium
Target Milestone: ---
Assignee: Charalampos Stratakis
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2015-03-14 11:17 UTC by David Gibson
Modified: 2016-07-20 00:38 UTC (History)
5 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2016-07-19 19:33:07 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)

Description David Gibson 2015-03-14 11:17:14 UTC
Description of problem:

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.

Version-Release number of selected component (if applicable):

python3-3.4.1-16.fc21.x86_64

How reproducible:

100%

Steps to Reproduce:
1.  Enter the following Python sample code

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)


2. Run the sample code

Actual results:

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)

Expected results:

The assertion should not trip, and inspect.signature(baz) should be the same as inspect.signature(f.bar)

Additional info:

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.

Comment 1 David Gibson 2015-03-14 11:18:13 UTC
I've had a look at the upstream cpython hg tree.  inspect.py has been rearranged a fair bit, but it looks like the bug is still present there.

I'm looking into getting the right credentials to file the bug in the upstream tracker.

Comment 2 Fedora Admin XMLRPC Client 2015-05-12 12:02:31 UTC
This package has changed ownership in the Fedora Package Database.  Reassigning to the new owner of this component.

Comment 3 Petr Viktorin (pviktori) 2015-05-18 07:51:59 UTC
David, are you still on this? I can forward this upstream if you don't have time to get your credentials.

Comment 4 David Gibson 2015-05-25 04:47:09 UTC
Sorry, I've been away on holiday the last 2 weeks.  I'm back now.

Please do forward this upstream, that would be very helpful.

Comment 5 Petr Viktorin (pviktori) 2015-05-27 15:34:01 UTC
Reported as http://bugs.python.org/issue24298

Comment 6 David Gibson 2015-07-09 04:01:50 UTC
Hi, I see this from that link that the bug has now been resolved upstream.

I've checked and the bug is still present in Fedora 22 - any idea on how long this will take to be folded back into Fedora?

Comment 7 Petr Viktorin (pviktori) 2015-07-09 08:32:37 UTC
It's been resolved, but's not yet released. In Fedora we generally avoid backporting individual patches.

There's no info upstream about 3.4.4 schedule, unfortunately.

Comment 8 Fedora Admin XMLRPC Client 2016-01-29 13:08:08 UTC
This package has changed ownership in the Fedora Package Database.  Reassigning to the new owner of this component.

Comment 9 Fedora End Of Life 2016-07-19 19:33:07 UTC
Fedora 22 changed to end-of-life (EOL) status on 2016-07-19. Fedora 22 is
no longer maintained, which means that it will not receive any further
security or bug fix updates. As a result we are closing this bug.

If you can reproduce this bug against a currently maintained version of
Fedora please feel free to reopen this bug against that version. If you
are unable to reopen this bug, please file a new report against the
current release. If you experience problems, please add a comment to this
bug.

Thank you for reporting this bug and we are sorry it could not be fixed.

Comment 10 David Gibson 2016-07-20 00:38:16 UTC
Appears to be fixed in Fedora 24.


Note You need to log in before you can comment on or make changes to this bug.