Issue 3354: Improve error reporting for the argument parsing C API - Python tracker

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
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Ankur.Ankan, benjamin.peterson, cito, iritkatriel, larry, mishok13, rhettinger, ronaldoussoren
Priority: low Keywords:

Created on 2008-07-14 12:53 by cito, last changed 2022-04-11 14:56 by admin.

Messages (9)
msg69651 - (view) Author: Christoph Zwerschke (cito) * Date: 2008-07-14 12:53
When you sort a list with list.sort() or sorted(list), and set the
reverse parameter to None, then you get the following misleading error
message:

    TypeError: an integer is required

I would expect a more proper error message for the reverse parameter,
such as "reverse must be a boolean", and maybe reverse=None also
accepted as default value, i.e. False.
msg69653 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-07-14 13:30
Well, booleans technically are integers.
msg69657 - (view) Author: Christoph Zwerschke (cito) * Date: 2008-07-14 15:19
The problem is not only that the error message "TypeError: an integer is
required" has "integer" instead of "boolean", but it does not mention
the attribute name "reverse", i.e. it does not even say *where* the
integer is required. I firmly believe error messages should not only be
technically correct, but also precise, meaningful and helpful for the user.
msg69658 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-07-14 15:22
Patches are welcome.
msg69659 - (view) Author: Christoph Zwerschke (cito) * Date: 2008-07-14 18:22
The unspecific error message is thrown by the vgetargskeywords()
function, so changing this behavior would be a larger, more difficult
and general issue (similar to #1283289). We could go another path and
require an object for 'reverse' instead of an integer in the format
parameter, and then do our own checks and error messages on 'reverse'.
This looks reasonable if we want to change the behavior anyway, so that
reverse=None is accepted (cmp=None and key=None are also accepted), or
even an implicit bool(reverse) is used (but this could hide possible
errors). If we want to keep the behavior and only fix the error message,
then a more general fix of vgetargskeywords() seems to be the proper
solution.
msg69662 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-07-14 21:44
I think this is closer to a language wide change and should probably be 
addressed for 2.7 and 3.1.  It would be great to change the C argument 
parsing API to make its error messages more specific.

For Py2.6, I think things are fine as it stands.  This isn't a bug 
(tests pass, it matches results from previous pythons, etc)  
Reclassifying as a feature request for improved error reports for the 
argument parsing API.
msg69669 - (view) Author: Christoph Zwerschke (cito) * Date: 2008-07-14 23:40
Agree. Seems to be a more general weakness of the argument parsing of
builtin functions and methods, that calls for a general solution instead
of a local patch. Luckily there are not so many cases where the errors
are misleading, since the builtin functions have very few and mostly
positioned parameters, so the problem is not as bad as it seems. There
may be only very few cases, like the example above, where the errors are
really too unspecific or misleading.
msg191584 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2013-06-21 14:34
See also #18269.
msg407295 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-29 16:50
Reproduced in 3.11:

>>> sorted([1,2,3], reverse=None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object cannot be interpreted as an integer