classification
Title: inspect.getcallargs raises TypeError on valid arguments
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Trundle, benjamin.peterson, daniel.urban, gsakkis, python-dev
Priority: normal Keywords: patch

Created on 2011-02-20 12:11 by daniel.urban, last changed 2011-03-28 22:41 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
issue11256.diff daniel.urban, 2011-02-20 13:32 Patch (py3k branch) review
issue11256_2.diff daniel.urban, 2011-02-26 12:54 Patch with more tests review
issue11256_py27.patch Trundle, 2011-02-27 03:20 review
issue11256_3.diff daniel.urban, 2011-02-27 10:09 New patch with corrected fix and more tests review
issue11256_4.patch daniel.urban, 2011-03-18 17:20 Patch for the new mercurial repository review
Messages (7)
msg128902 - (view) Author: Daniel Urban (daniel.urban) * Date: 2011-02-20 12:11
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.
msg128907 - (view) Author: Daniel Urban (daniel.urban) * Date: 2011-02-20 13:32
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.
msg129538 - (view) Author: Daniel Urban (daniel.urban) * Date: 2011-02-26 12:54
Updated patch with extra tests.
msg129608 - (view) Author: Andreas Stührk (Trundle) Date: 2011-02-27 03:20
Confirmed under Python 2.7, 3.2 and 3.3. Patch looks good to me. Attached is a patch for 2.7.
msg129620 - (view) Author: Daniel Urban (daniel.urban) * Date: 2011-02-27 10:09
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.
msg131340 - (view) Author: Daniel Urban (daniel.urban) * Date: 2011-03-18 17:20
Updated the patch for mercurial.
msg132439 - (view) Author: Roundup Robot (python-dev) Date: 2011-03-28 22:41
New changeset 57e99f5f5e8f by Benjamin Peterson in branch '3.2':
Correct handling of functions with only kwarg args in getcallargs (closes #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 #11256)
http://hg.python.org/cpython/rev/b19d76d9d2a7
History
Date User Action Args
2011-03-28 22:41:06python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg132439

resolution: fixed
stage: resolved
2011-03-18 17:20:31daniel.urbansetfiles: + issue11256_4.patch
nosy: gsakkis, benjamin.peterson, Trundle, daniel.urban
messages: + msg131340
2011-02-27 10:09:12daniel.urbansetfiles: + issue11256_3.diff
nosy: gsakkis, benjamin.peterson, Trundle, daniel.urban
messages: + msg129620
2011-02-27 03:20:32Trundlesetfiles: + issue11256_py27.patch
nosy: + Trundle
messages: + msg129608

2011-02-26 12:54:19daniel.urbansetfiles: + issue11256_2.diff
nosy: gsakkis, benjamin.peterson, daniel.urban
messages: + msg129538
2011-02-20 13:32:18daniel.urbansetfiles: + issue11256.diff

messages: + msg128907
keywords: + patch
nosy: gsakkis, benjamin.peterson, daniel.urban
2011-02-20 12:11:08daniel.urbancreate