-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
argparse fails to parse [] when using choices and nargs='*' #71414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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
The fix seems to be as simple as moving the check under (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.) |
Looking at this again, I think we should fix this in bpo-9625. |
I don't think that this bpo-27227 and bpo-9625 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 bpo-27227 is that argparse does not accept 0 options is '0 or more' are specified in nargs. bpo-9625 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. |
Reproduced on 3.11. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: