Skip to content

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

Closed

Description

@evanunderscore
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']

    Activity

    1. evanunderscore commented on Jun 5, 2016

      evanunderscoremannequin
      MannequinAuthor

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

    2. added
      stdlib
      type-bug
      on Jun 5, 2016
    3. berkerpeksag commented on Jun 12, 2016

      @berkerpeksag
      Member

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

    4. macfreek commented on Apr 26, 2018

      macfreekmannequin
      Mannequin

      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.

    5. reopened this on Aug 31, 2019
    6. iritkatriel commented on Dec 11, 2021

      @iritkatriel
      Member

      Reproduced on 3.11.

    7. added
      3.9 (EOL)
      3.10 (EOL)
      3.11
      and removed
      3.7 (EOL)
      on Dec 11, 2021
    8. transferred this issue fromon Apr 10, 2022
    9. savannahostrowski commented on Sep 11, 2024

      @savannahostrowski
      Member

      This is a duplicate of #92445, which was fixed with ad7340e.

    10. moved this from Bugs to Doc issues in Argparse issueson Sep 11, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)3.113.9 (EOL)stdlibtype-bug

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions