Terry J. Reedy added the comment:

_Actions_container(object) [1198 in 3.3.0 code] .add_argument() [1281] does not 
directly check for choices.__iter__ ('__iter__' is not in the file). Nor does 
it use the 3.x-only alternative isinstance(choices, collections) ('Iterable' is 
also not in the file). Rather it formats the help string for each argument as 
added.

The complete statement that raises the error is (now at 1321):
    try:
        self._get_formatter()._format_args(action, None)
    except TypeError:
        raise ValueError("length of metavar tuple does not match nargs")

def _get_formatter is part of
class ArgumentParser(_AttributeHolder, _ActionsContainer):
so 'self' has to be an ArguementParser for the above exception.

_format_args calls get_metavar, which is returned by _metavar_formatter. The 
last contains the first of the three choices iterations that I propose to 
factor out and revise. So that proposal should eliminate the particular 
exception from add_argument.

The docstring for class Action mirrors the doc:
'''
-  choices -- A container of values that should be allowed. If not None,
   after a command-line argument has been converted to the appropriate
   type, an exception will be raised if it is not a member of this
   collection.
'''
This directly translates to the code line
    if action.choices is not None and value not in action.choices:

Trying to prettily format messages peripheral to the main goal should not take 
indefinitely or infinitely long, nor make the message worse, nor raise an 
exception.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue16468>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to