Skip to content
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

argparse fails to parse [] when using choices and nargs='*' #71414

Open
evanunderscore mannequin opened this issue Jun 5, 2016 · 4 comments
Open

argparse fails to parse [] when using choices and nargs='*' #71414

evanunderscore mannequin opened this issue Jun 5, 2016 · 4 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@evanunderscore
Copy link
Mannequin

evanunderscore mannequin commented Jun 5, 2016

BPO 27227
Nosy @macfreek, @berkerpeksag, @evanunderscore, @iritkatriel
Superseder
  • bpo-9625: argparse: Problem with defaults for variable nargs when using choices
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2016-06-05.08:59:16.765>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = "argparse fails to parse [] when using choices and nargs='*'"
    updated_at = <Date 2021-12-11.18:35:18.463>
    user = 'https://github.com/evanunderscore'

    bugs.python.org fields:

    activity = <Date 2021-12-11.18:35:18.463>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2016-06-05.08:59:16.765>
    creator = 'evan_'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 27227
    keywords = []
    message_count = 4.0
    messages = ['267404', '268387', '315783', '408330']
    nosy_count = 5.0
    nosy_names = ['macfreek', 'berker.peksag', 'paul.j3', 'evan_', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'open'
    superseder = '9625'
    type = 'behavior'
    url = 'https://bugs.python.org/issue27227'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @evanunderscore
    Copy link
    Mannequin Author

    evanunderscore mannequin commented Jun 5, 2016

    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.)

    @evanunderscore evanunderscore mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 5, 2016
    @berkerpeksag
    Copy link
    Member

    Looking at this again, I think we should fix this in bpo-9625.

    @macfreek
    Copy link
    Mannequin

    macfreek mannequin commented Apr 26, 2018

    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.

    @macfreek macfreek mannequin added the 3.7 (EOL) end of life label Apr 26, 2018
    @paulj3 paulj3 mannequin reopened this Aug 31, 2019
    @iritkatriel
    Copy link
    Member

    Reproduced on 3.11.

    @iritkatriel iritkatriel added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes and removed 3.7 (EOL) end of life labels Dec 11, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    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 stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    Status: Bugs
    Development

    No branches or pull requests

    2 participants