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
Title: argparse fails to parse [] when using choices and nargs='*'
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution: duplicate
Dependencies: Superseder: argparse: Problem with defaults for variable nargs when using choices
View: 9625
Assigned To: Nosy List: berker.peksag, evan_, iritkatriel, macfreek, paul.j3
Priority: normal Keywords:

Created on 2016-06-05 08:59 by evan_, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg267404 - (view) Author: Evan Andrews (evan_) * Date: 2016-06-05 08:59
When using nargs='*' with choices, it is impossible to specify 0 args:

from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('foo', choices=['foo'], nargs='*')
args = parser.parse_args([])  # <-- fails, error message below
assert args.foo == []
# usage: args.py [-h] [{foo} [{foo} ...]]
# args.py: error: argument foo: invalid choice: [] (choose from 'foo')

The problem appears to be this block of code trying to validate `value` immediately after it sets it to `[]`:

        # when nargs='*' on a positional, if there were no command-line
        # args, use the default if it is anything other than None
        elif (not arg_strings and action.nargs == ZERO_OR_MORE and
              not action.option_strings):
            if action.default is not None:
                value = action.default
            else:
                value = arg_strings
            self._check_value(action, value)

The fix seems to be as simple as moving the check under `if action.default is not None`.

(NOTE: This would be also adequately solved by patches already attached to http://bugs.python.org/issue9625, however the minimal solution to this problem is simpler.)
msg268387 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-06-12 14:30
Looking at this again, I think we should fix this in issue 9625.
msg315783 - (view) Author: Freek Dijkstra (macfreek) Date: 2018-04-26 10:00
I don't think that this issue27227 and issue9625 are the same (although they may have the same root cause, and I would be in favour of fixing both issues). At least, I think the unit test are distinct:

This issue27227 is that argparse does not accept 0 options is '0 or more' are specified in nargs. Issue9625 is that argparse returns a different type if a default is specified or when it is not.

So I propose to reopen this ticket: it is not fixed, and different from the referred ticket.
msg408330 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-11 18:35
Reproduced on 3.11.
History
Date User Action Args
2022-04-11 14:58:32adminsetgithub: 71414
2021-12-11 18:35:18iritkatrielsetnosy: + iritkatriel

messages: + msg408330
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.5, Python 3.6, Python 3.7
2019-08-31 16:17:21paul.j3setstatus: closed -> open
nosy: + paul.j3
2018-04-26 10:00:51macfreeksetnosy: + macfreek

messages: + msg315783
versions: + Python 3.7
2016-06-12 14:30:29berker.peksagsetstatus: open -> closed
superseder: argparse: Problem with defaults for variable nargs when using choices
messages: + msg268387

resolution: duplicate
stage: needs patch -> resolved
2016-06-07 04:05:40berker.peksagsetnosy: + berker.peksag
stage: needs patch

versions: - Python 3.4
2016-06-05 08:59:16evan_create