[New-bugs-announce] [issue16468] argparse only supports iterable choices

Chris Jerdonek report at bugs.python.org
Wed Nov 14 05:32:19 CET 2012

New submission from Chris Jerdonek:

This issue is to ensure that argparse.ArgumentParser() accepts objects that support the "in" operator for the "choices" argument to ArgumentParser.add_argument().

As observed by Terry in the comments to issue 16418:


the argparse module does not in general support "choices" values that support the "in" operator, even though the argparse documentation says it does:

"Any object that supports the in operator can be passed as the choices value, so dict objects, set objects, custom containers, etc. are all supported."

(from http://docs.python.org/2/library/argparse.html#choices )

For example, passing a user-defined type that implements only self.__contains__() yields the following error message when calling ArgumentParser.parse_args():

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

(The error message also gives the wrong reason for failure.  The swallowed exception is "TypeError: '<class>' object is not iterable.")

components: Library (Lib)
messages: 175534
nosy: chris.jerdonek, terry.reedy
priority: normal
severity: normal
status: open
title: argparse only supports iterable choices
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list