This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyArg_ParseTupleAndKeywords gives misleading error message
Type: Stage:
Components: Extension Modules Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: loewis Nosy List: ajaksu2, georg.brandl, georg.brandl, japierro, loewis
Priority: normal Keywords:

Created on 2005-09-06 19:12 by japierro, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg26218 - (view) Author: japierro (japierro) Date: 2005-09-06 19:12
Python 2.3.3 on Windows XP

If the user supplies one valid and one invalid keyword to 
a function that defines two required keyword arguments, 
PyArg_ParseTupleAndKeywords will raise this 
exception:

TypeError: function takes exactly 2 arguments (1 given)

If, in getargs.c, vgetargskeywords, the check 
for "extraneous keywords" were done near the top of the 
function, a more meaningful exception would be thrown:

TypeError: 'bad' is an invalid keyword argument for this 
function
msg26219 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-12-16 19:43
Logged In: YES 
user_id=1188172

Any opinions?
msg26220 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-12-27 13:48
Logged In: YES 
user_id=21627

I agree that the error message should be fixed; this is even
an 'XXX' comment ("an this isn't a bug?").

Unfortunately, just looking for bad keyword arguments is not
enough - it might even be that an optional keyword argument
is supplied, and still the error message is confusing, e.g. for

>>> re.compile("a").match(pos=10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: match() takes at least 1 argument (0 given)

Instead, I think the error message should read: 

TypeError: "pattern" argument for match() is missing

as we know exactly what "meaning" the missing argument has.

This might still be unspecific if multiple required
arguments are missing, but I think just giving the first one
would be informative enough - or else it could read

TypeError: "pattern" argument for match() is missing (+2 more)

if there are 2 more missing.

Yet alternatively, it could read

TypeError: required arguments for match() missing:
'pattern', 'foo', 'bar'

Patches in this direction are welcome; the precise wording
proposal should be mentioned on python-dev.
msg83876 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-03-20 22:31
Fixed on trunk and py3k, can someone test on the release branches?

Python 2.7a0 (trunk, Feb 24 2009, 10:30:17)
>>> re.compile("a").match(pos=10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Required argument 'pattern' (pos 1) not found
msg85501 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-04-05 14:31
Looks like the new message is in 2.6 as well.
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42347
2009-04-05 14:31:48georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg85501

resolution: fixed
2009-03-20 22:31:06ajaksu2setnosy: + ajaksu2

messages: + msg83876
versions: + Python 2.6, Python 3.0
2005-09-06 19:12:46japierrocreate