Skip to content

argparse: action "extend" with 1 parameter splits strings into characters #84545

Closed

Description

@strjan
mannequin
BPO 40365
Nosy @rhettinger, @berkerpeksag, @asottile, @isidentical, @jonathanhaigh
Files
  • argparse_bug.py: Minimal example
  • 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 2020-04-22.17:42:36.895>
    labels = ['type-bug', '3.8', '3.9']
    title = 'argparse: action "extend" with 1 parameter splits strings into characters'
    updated_at = <Date 2020-06-09.13:20:45.393>
    user = 'https://bugs.python.org/strjan'

    bugs.python.org fields:

    activity = <Date 2020-06-09.13:20:45.393>
    actor = 'Jonathan Haigh'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2020-04-22.17:42:36.895>
    creator = 'strjan'
    dependencies = []
    files = ['49086']
    hgrepos = []
    issue_num = 40365
    keywords = []
    message_count = 4.0
    messages = ['367033', '367045', '370898', '371103']
    nosy_count = 7.0
    nosy_names = ['rhettinger', 'berker.peksag', 'paul.j3', 'Anthony Sottile', 'BTaskaya', 'strjan', 'Jonathan Haigh']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40365'
    versions = ['Python 3.8', 'Python 3.9']

    Linked PRs

    Activity

    1. strjan commented on Apr 22, 2020

      strjanmannequin
      MannequinAuthor

      When positional argument with action='extend' is given to parse only one string argument, the string is splitted into individual characters.

      The problem occures, when no "nargs" is given; it makes sense that nargs is requiered, though at least notion in documentation would be nice, as the issue does not occure with action='append'

      Minimal example included.

    2. added
      3.8 (EOL)
      3.9 (EOL)
      type-bug
      on Apr 22, 2020
    3. paulj3 commented on Apr 22, 2020

      paulj3mannequin
      Mannequin

      This is a consequence of Python's own definition of append vs extend

      In [730]: alist = []
      In [731]: alist.append('astring')
      In [732]: alist
      Out[732]: ['astring']
      In [733]: alist.extend('astring')
      In [734]: alist
      Out[734]: ['astring', 'a', 's', 't', 'r', 'i', 'n', 'g']
      In [735]: alist.extend(['astring'])
      In [736]: alist
      Out[736]: ['astring', 'a', 's', 't', 'r', 'i', 'n', 'g', 'astring']

      Normally 'add_argument' doesn't check for valid parameters, but some Action subclasses do their own checking,

      'extend' inherits the nargs==0 test from 'append'. (extend just modifies the __call__, not the __init__ method.

      But I wonder, was this situation discussed in the original bug/issue?

    4. jonathanhaigh commented on Jun 7, 2020

      jonathanhaighmannequin
      Mannequin

      The situation for type=int and unspecified nargs or nargs="?" is also surprising:

      Python 3.8.3 (default, May 21 2020, 12:19:36) 
      [GCC 9.2.1 20191008] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import argparse
      >>> p = argparse.ArgumentParser()
      >>> p.add_argument("--c", action="extend", type=int)
      _ExtendAction(option_strings=['--c'], dest='c', nargs=None, const=None, default=None, type=<class 'int'>, choices=None, help=None, metavar=None)
      >>> p.parse_args("--c 1".split())
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1768, in parse_args
          args, argv = self.parse_known_args(args, namespace)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1800, in parse_known_args
          namespace, args = self._parse_known_args(args, namespace)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 2006, in _parse_known_args
          start_index = consume_optional(start_index)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1946, in consume_optional
          take_action(action, args, option_string)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1874, in take_action
          action(self, namespace, argument_values, option_string)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1171, in __call__
          items.extend(values)
      TypeError: 'int' object is not iterable
      >>> p = argparse.ArgumentParser()
      >>> p.add_argument("--c", action="extend", type=int, nargs="?")
      _ExtendAction(option_strings=['--c'], dest='c', nargs='?', const=None, default=None, type=<class 'int'>, choices=None, help=None, metavar=None)
      >>> p.parse_args("--c 1".split())
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1768, in parse_args
          args, argv = self.parse_known_args(args, namespace)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1800, in parse_known_args
          namespace, args = self._parse_known_args(args, namespace)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 2006, in _parse_known_args
          start_index = consume_optional(start_index)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1946, in consume_optional
          take_action(action, args, option_string)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1874, in take_action
          action(self, namespace, argument_values, option_string)
        File "/home/jonathan/.pyenv/versions/3.8.3/lib/python3.8/argparse.py", line 1171, in __call__
          items.extend(values)
      TypeError: 'int' object is not iterable
      >>>

      I suggest that the default nargs for extend should be "*" or "+" and an exception should be raised if nargs is given as "?". I don't see the current behaviour with unspecified nargs or nargs="?" being useful (and it certainly is surprising). In both cases, I think the least surprising behaviour would be for extend to act the same as append (or for an exception to be raised).

      But I wonder, was this situation discussed in the original bug/issue?
      Doesn't look like it:
      https://bugs.python.org/issue23378
      aa32a7e

    5. jonathanhaigh commented on Jun 9, 2020

      jonathanhaighmannequin
      Mannequin

      > But I wonder, was this situation discussed in the original bug/issue?
      Doesn't look like it:

      I was looking at the wrong PR link. This has more discussion: #13305.

      nargs is discussed but I'm not sure it was realized that the nargs=None and nargs="?" cases would act in the way seen here rather than acting like append.

      Having a default nargs of "+" was suggested but that suggestion was not addressed.

      I suggest that the default nargs for extend should be "*" or "+" and an exception should be raised if nargs is given as "?".

      I'm not convinced about that any more. Using append's behaviour is probably more reasonable for nargs=None and nargs="?".

    6. transferred this issue fromon Apr 10, 2022
    7. added
      stdlib
      on Nov 23, 2023
    8. serhiy-storchaka commented on Sep 25, 2024

      @serhiy-storchaka
      Member

      I do not think that we should forbid nargs=None and nargs='?'. It may be surprising for some users when they experience this the first time, but it can be the needed behavior for others. And it can already be used in the user code. This is why it is even hard to change the default value of nargs.

      But these peculiarities should be documented.

    9. 1 remaining item

    10. added
      docs
      3.12
      3.13
      3.14
      and removed
      3.9 (EOL)
      3.8 (EOL)
      on Sep 25, 2024
    11. added 2 commits that reference this issue on Oct 23, 2024
    12. added 2 commits that reference this issue on Oct 25, 2024
    13. added 2 commits that reference this issue on Oct 26, 2024
    14. moved this from Bugs to Doc issues in Argparse issueson Oct 26, 2024
    15. added a commit that references this issue on Jan 12, 2025
    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.123.133.14docsstdlib

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions