classification
Title: inspect.getcallargs() attempts to iterate over None
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: jlowin, ncoghlan, python-dev, yselivanov
Priority: normal Keywords: patch

Created on 2014-03-01 16:00 by jlowin, last changed 2014-03-27 22:28 by yselivanov. This issue is now closed.

Files
File name Uploaded Description Edit
issue20816.patch jlowin, 2014-03-01 16:08 review
Messages (5)
msg212509 - (view) Author: Jeremiah Lowin (jlowin) * Date: 2014-03-01 16:00
Tested in Python 3.3 and Python 3.4.0rc1 5e05d7d3db9c

If a function has keyword-only arguments but no keyword-only defaults, then calling inspect.getcallargs with no arguments results in the wrong TypeError being raised.

Example:

>>> import inspect
>>> def fn(*, a): 
>>>    pass
>>> inspect.getcallargs(fn)

Result: 
    TypeError: argument of type 'NoneType' is not iterable

Expected Result: 
    TypeError: fn() missing 1 required keyword-only argument: 'a'
msg212512 - (view) Author: Jeremiah Lowin (jlowin) * Date: 2014-03-01 16:07
I created a patch to resolve this. 

If a function has keyword-only arguments, then inspect.getcallargs checks if the argument is in kwonlydefaults. However, kwonlydefaults is None if no defaults were specified. In that situation, 'kwarg in kwonlydefaults' raises the TypeError.

The quick fix is simply to test kwonlydefaults before testing if kwarg is in it.

The test for this situation is a little verbose because a TypeError is expected and one is raised, just the wrong one, so I parse the error message.
msg212513 - (view) Author: Jeremiah Lowin (jlowin) * Date: 2014-03-01 16:08
Apologies, the patch is attached here.
msg214986 - (view) Author: Roundup Robot (python-dev) Date: 2014-03-27 22:28
New changeset 3de2e729d0fb by Yury Selivanov in branch 'default':
inspect: Fix getcallargs() to raise correct TypeError
http://hg.python.org/cpython/rev/3de2e729d0fb

New changeset 070dfca74610 by Yury Selivanov in branch '3.4':
inspect: Fix getcallargs() to raise correct TypeError
http://hg.python.org/cpython/rev/070dfca74610
msg214987 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-03-27 22:28
Fixed for 3.4.1 and 3.5.
Thank you Jeremiah!
History
Date User Action Args
2014-03-27 22:28:51yselivanovsetmessages: + msg214987
2014-03-27 22:28:05python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg214986

resolution: fixed
stage: resolved
2014-03-01 18:40:24yselivanovsetassignee: yselivanov

nosy: + ncoghlan
2014-03-01 16:08:37jlowinsetfiles: + issue20816.patch
keywords: + patch
messages: + msg212513
2014-03-01 16:07:45jlowinsetmessages: + msg212512
2014-03-01 16:05:37brett.cannonsetnosy: + yselivanov
2014-03-01 16:00:50jlowincreate