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.

Author terry.reedy
Recipients bethard, chris.jerdonek, r.david.murray, terry.reedy, wim.glenn
Date 2013-01-16.07:00:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1358319618.78.0.708035241109.issue16468@psf.upfronthosting.co.za>
In-reply-to
Content
_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.
History
Date User Action Args
2013-01-16 07:00:18terry.reedysetrecipients: + terry.reedy, bethard, r.david.murray, chris.jerdonek, wim.glenn
2013-01-16 07:00:18terry.reedysetmessageid: <1358319618.78.0.708035241109.issue16468@psf.upfronthosting.co.za>
2013-01-16 07:00:18terry.reedylinkissue16468 messages
2013-01-16 07:00:17terry.reedycreate