Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspect.signature doesn't always return a signature #66000

Closed
PCManticore mannequin opened this issue Jun 18, 2014 · 5 comments
Closed

inspect.signature doesn't always return a signature #66000

PCManticore mannequin opened this issue Jun 18, 2014 · 5 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@PCManticore
Copy link
Mannequin

PCManticore mannequin commented Jun 18, 2014

BPO 21801
Nosy @ncoghlan, @larryhastings, @PCManticore, @1st1
Files
  • inspect_signature.patch
  • issue21801.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/1st1'
    closed_at = <Date 2014-06-23.17:24:02.008>
    created_at = <Date 2014-06-18.12:41:45.841>
    labels = ['type-feature', 'library']
    title = "inspect.signature doesn't always return a signature"
    updated_at = <Date 2014-06-23.17:24:36.614>
    user = 'https://github.com/PCManticore'

    bugs.python.org fields:

    activity = <Date 2014-06-23.17:24:36.614>
    actor = 'yselivanov'
    assignee = 'yselivanov'
    closed = True
    closed_date = <Date 2014-06-23.17:24:02.008>
    closer = 'python-dev'
    components = ['Library (Lib)']
    creation = <Date 2014-06-18.12:41:45.841>
    creator = 'Claudiu.Popa'
    dependencies = []
    files = ['35682', '35710']
    hgrepos = []
    issue_num = 21801
    keywords = ['patch', 'needs review']
    message_count = 5.0
    messages = ['220936', '221103', '221114', '221368', '221369']
    nosy_count = 5.0
    nosy_names = ['ncoghlan', 'larry', 'Claudiu.Popa', 'python-dev', 'yselivanov']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue21801'
    versions = ['Python 3.4', 'Python 3.5']

    @PCManticore
    Copy link
    Mannequin Author

    PCManticore mannequin commented Jun 18, 2014

    Hello. I noticed the following behaviour while working with xmlrpc proxy methods.

    >>> import inspect, xmlrpc.client
    >>> proxy = xmlrpc.client.ServerProxy('http://localhost:8000')
    >>> proxy.mul
    <xmlrpc.client._Method object at 0x03B0C890>
    >>> inspect.signature(proxy.mul)
    <xmlrpc.client._Method object at 0x03B0CC90>
    >>>

    Now, according to the documentation, inspect.signature should return a signature or fail, but in this case it returns the actual object, which is misleading at least. This happens because _Method implements a proxy __getattr__, any accessed attribute becoming again a _Method. At the same time, inspect.signature happily uses

    try:
    obj.__signature__
    except AttributeError:
    ...

    to obtain the signature, if present.

    The attached patch checks if __signature__ is an actual Signature object. I searched, but couldn't find any, if __signature__ can be anything, so I hope that this approach works.

    @PCManticore PCManticore mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jun 18, 2014
    @1st1
    Copy link
    Member

    1st1 commented Jun 20, 2014

    This behaviour is indeed a bug. However, I think that the solution you propose is wrong.

    If we ignore invalid contents of __signature__ we are masking a bug or incorrect behaviour. In this case, you should have checked the requested attribute name in '__getattr__', and return something other than _Method, if it is a '__signature__'.

    Please find attached a patch, that checks if __signature__ is an instance of Signature class, and raises a TypeError if it isn't.

    @PCManticore
    Copy link
    Mannequin Author

    PCManticore mannequin commented Jun 20, 2014

    Your patch looks good to me.

    @1st1 1st1 self-assigned this Jun 20, 2014
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 23, 2014

    New changeset cc0f5d6ccb70 by Yury Selivanov in branch '3.4':
    inspect: Validate that __signature__ is None or an instance of Signature.
    http://hg.python.org/cpython/rev/cc0f5d6ccb70

    New changeset fa5b985f0920 by Yury Selivanov in branch 'default':
    inspect: Validate that __signature__ is None or an instance of Signature.
    http://hg.python.org/cpython/rev/fa5b985f0920

    @python-dev python-dev mannequin closed this as completed Jun 23, 2014
    @1st1
    Copy link
    Member

    1st1 commented Jun 23, 2014

    Fixed in 3.4 and 3.5.
    Thanks for the bug report!

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant