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
Comments
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. |
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. |
Updated patch with extra tests. |
Confirmed under Python 2.7, 3.2 and 3.3. Patch looks good to me. Attached is a patch for 2.7. |
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. |
Updated the patch for mercurial. |
New changeset 57e99f5f5e8f by Benjamin Peterson in branch '3.2': New changeset b19d76d9d2a7 by Benjamin Peterson in branch '2.7': |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: