[issue16468] argparse only supports iterable choices

Chris Jerdonek report at bugs.python.org
Tue Jan 15 12:36:52 CET 2013


Chris Jerdonek added the comment:

Attaching patch.  With this patch, passing a non-iterable choices argument to parser.add_argument() raises (for example):

Traceback (most recent call last):
  ...
  File ".../Lib/argparse.py", line 558, in _metavar_formatter
    choice_strs = [str(choice) for choice in action.choices]
TypeError: 'MyChoices' object is not iterable

instead of the incorrect:

  File ".../Lib/argparse.py", line 1333, in add_argument
    raise ValueError("length of metavar tuple does not match nargs")
ValueError: length of metavar tuple does not match nargs

Is it okay to change this exception type in maintenance releases?  The other option is to keep the error as a ValueError but to change the error message, though I think TypeError is the correct exception to allow through.  Note that the existing ValueError is preserved for other code paths.  Indeed, there are several tests checking for this ValueError and its error message, which the attached patch does not break.

If we want to consider accepting non-iterable choices for 3.4, we can still have that discussion as part of a separate patch.

----------
stage: needs patch -> patch review
Added file: http://bugs.python.org/file28734/issue-16468-2.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16468>
_______________________________________


More information about the Python-bugs-list mailing list