Skip to content

Incorrect interaction between argparse nargs='*' and choices #92445

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

Closed
rhettinger opened this issue May 8, 2022 · 3 comments
Closed

Incorrect interaction between argparse nargs='*' and choices #92445

rhettinger opened this issue May 8, 2022 · 3 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes 3.12 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@rhettinger
Copy link
Contributor

In the code below, it is valid to supply no arguments.

from argparse import ArgumentParser

p = ArgumentParser()
p.add_argument('dessert', nargs='*', choices=('cake', 'pie'))
p.parse_args([])

However, the following error is generated:

usage: tmp5.py [-h] [{cake,pie} ...]
tmp5.py: error: argument dessert: invalid choice: [] (choose from 'cake', 'pie')

What is happening is that the empty list is being treated a value rather than a collection of values.

@rhettinger rhettinger added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir 3.11 only security fixes easy 3.10 only security fixes 3.9 only security fixes 3.12 only security fixes labels May 8, 2022
@AbhigyanBose
Copy link
Contributor

@rhettinger

Could you please elaborate the expected behavior here ?

I tested this on 3.8 and the main branch. In both p.parse_args([]) gives us the same result as calling the script without any arguments.

Could you tell me the expected output should the code snippet you shared(also pasted below) be run without any arguments?

from argparse import ArgumentParser

p = ArgumentParser()
p.add_argument('dessert', nargs='*', choices=('cake', 'pie'))
p.parse_args([])

@Harry-Lees
Copy link
Contributor

Could you please elaborate the expected behavior here ?

I believe the expected behaviour for passing an empty list with nargs="*" should be the same behaviour as passing an empty list with nargs="?" since * is 0 or more arguments whereas ? is 0 or 1 arguments.

>>> from argparse import ArgumentParser
>>> p = ArgumentParser()
>>> p.add_argument('dessert', nargs="?", choices=('cake', 'pie'))
>>> p.parse_args([])
Namespace(dessert=None)

@hpaulj
Copy link

hpaulj commented May 10, 2022

This only happens for a positional. nargs='*' (and '?') gets some special handling in get_values, when an emply list satisfies its nargs. The default is assigned in place of the empty list.

This is part of the logic that allows us to use these positionals in a mutually_exclusive_group.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes 3.12 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: Doc issues
Development

No branches or pull requests

4 participants