Issue 20816: inspect.getcallargs() attempts to iterate over None (original) (raw)

Created on 2014-03-01 16:00 by jlowin, last changed 2022-04-11 14:57 by admin. 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) (Python triager) 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
2022-04-11 14:57:59 admin set github: 65015
2014-03-27 22:28:51 yselivanov set messages: +
2014-03-27 22:28:05 python-dev set status: open -> closednosy: + python-devmessages: + resolution: fixedstage: resolved
2014-03-01 18:40:24 yselivanov set assignee: yselivanovnosy: + ncoghlan
2014-03-01 16:08:37 jlowin set files: + issue20816.patchkeywords: + patchmessages: +
2014-03-01 16:07:45 jlowin set messages: +
2014-03-01 16:05:37 brett.cannon set nosy: + yselivanov
2014-03-01 16:00:50 jlowin create