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.getcallargs raises TypeError on valid arguments #55465

Closed
durban mannequin opened this issue Feb 20, 2011 · 7 comments
Closed

inspect.getcallargs raises TypeError on valid arguments #55465

durban mannequin opened this issue Feb 20, 2011 · 7 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@durban
Copy link
Mannequin

durban mannequin commented Feb 20, 2011

BPO 11256
Nosy @benjaminp, @Trundle, @durban
Files
  • issue11256.diff: Patch (py3k branch)
  • issue11256_2.diff: Patch with more tests
  • issue11256_py27.patch
  • issue11256_3.diff: New patch with corrected fix and more tests
  • issue11256_4.patch: Patch for the new mercurial repository
  • 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 = None
    closed_at = <Date 2011-03-28.22:41:06.233>
    created_at = <Date 2011-02-20.12:11:08.725>
    labels = ['type-bug', 'library']
    title = 'inspect.getcallargs raises TypeError on valid arguments'
    updated_at = <Date 2011-03-28.22:41:06.231>
    user = 'https://github.com/durban'

    bugs.python.org fields:

    activity = <Date 2011-03-28.22:41:06.231>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2011-03-28.22:41:06.233>
    closer = 'python-dev'
    components = ['Library (Lib)']
    creation = <Date 2011-02-20.12:11:08.725>
    creator = 'daniel.urban'
    dependencies = []
    files = ['20805', '20911', '20927', '20930', '21281']
    hgrepos = []
    issue_num = 11256
    keywords = ['patch']
    message_count = 7.0
    messages = ['128902', '128907', '129538', '129608', '129620', '131340', '132439']
    nosy_count = 5.0
    nosy_names = ['gsakkis', 'benjamin.peterson', 'Trundle', 'daniel.urban', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue11256'
    versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

    @durban
    Copy link
    Mannequin Author

    durban mannequin commented Feb 20, 2011

    inspect.getcallargs raises TypeError if given a function with only **kwargs, and some keyword arguments:

    Python 3.3a0 (py3k:88451, Feb 20 2011, 12:37:22) 
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    >>> from inspect import getcallargs
    >>> 
    >>> def f(**kwargs): pass
    ... 
    >>> f(a=1, b=2)
    >>> 
    >>> getcallargs(f, a=1, b=2)
    Traceback (most recent call last):
        ...
    TypeError: f() takes no arguments (2 given)

    In line 946 of inspect.py the "num_args == 0 and num_total" condition is true: the function expects 0 positional arguments and got more than zero arguments, but in this case these are keyword arguments, so it shouldn't raise TypeError.

    @durban durban mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 20, 2011
    @durban
    Copy link
    Mannequin Author

    durban mannequin commented Feb 20, 2011

    Here is a patch. It also includes tests that would have detected this bug. It also corrects a case when getcallargs raised an exception with a different message (there are tests also for this):

    >>> def f(**kwargs): pass
    ... 
    >>> f(1, a=2)
    Traceback (most recent call last):
        ...
    TypeError: f() takes exactly 0 positional arguments (2 given)
    >>> 
    >>> getcallargs(f, 1, a=2)
    Traceback (most recent call last):
        ...
    TypeError: f() takes no arguments (2 given)

    There is a comment in the patch about this case: the message given by Python is also incorrect, because it says that 2 positional arguments are given, but there was only 1 positional argument (the other was a keyword argument). The patch currently handles this case by producing the same (incorrect) message as Python.

    @durban
    Copy link
    Mannequin Author

    durban mannequin commented Feb 26, 2011

    Updated patch with extra tests.

    @Trundle
    Copy link
    Mannequin

    Trundle mannequin commented Feb 27, 2011

    Confirmed under Python 2.7, 3.2 and 3.3. Patch looks good to me. Attached is a patch for 2.7.

    @durban
    Copy link
    Mannequin Author

    durban mannequin commented Feb 27, 2011

    I found another case, when this is a problem: if there are no **kwargs, but there are some keyword-only arguments:

    >>> def f(*, a, b): pass
    ... 
    >>> f(a=1, b=2)
    >>> 
    >>> getcallargs(f, a=1, b=2)
    Traceback (most recent call last):
        ...
    TypeError: f() takes no arguments (2 given)

    The attached issue11256_3.diff patch also fixes this problem, and adds tests that would have detected this case.

    @durban
    Copy link
    Mannequin Author

    durban mannequin commented Mar 18, 2011

    Updated the patch for mercurial.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 28, 2011

    New changeset 57e99f5f5e8f by Benjamin Peterson in branch '3.2':
    Correct handling of functions with only kwarg args in getcallargs (closes bpo-11256)
    http://hg.python.org/cpython/rev/57e99f5f5e8f

    New changeset b19d76d9d2a7 by Benjamin Peterson in branch '2.7':
    Correct handling of functions with only kwarg args in getcallargs (closes bpo-11256)
    http://hg.python.org/cpython/rev/b19d76d9d2a7

    @python-dev python-dev mannequin closed this as completed Mar 28, 2011
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    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-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants