Skip to content

Argparse: Explicit default required arguments with add_mutually_exclusive_group are rejected #87386

Closed as not planned

Description

@keith
mannequin
BPO 43220
Nosy @rhettinger, @keith
PRs
  • bpo-43220: Accept explicit default args in required groups #24526
  • 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 2021-02-14.06:34:45.098>
    labels = ['3.8', 'library', '3.9', '3.10']
    title = 'Argparse: Explicit default required arguments with add_mutually_exclusive_group are rejected'
    updated_at = <Date 2021-05-12.05:00:45.027>
    user = 'https://github.com/keith'

    bugs.python.org fields:

    activity = <Date 2021-05-12.05:00:45.027>
    actor = 'rhettinger'
    assignee = 'paul.j3'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-02-14.06:34:45.098>
    creator = 'keith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43220
    keywords = ['patch']
    message_count = 5.0
    messages = ['386934', '386959', '390398', '390406', '390443']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'paul.j3', 'keith']
    pr_nums = ['24526']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue43220'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    Activity

    1. keith commented on Feb 14, 2021

      keithmannequin
      MannequinAuthor

      With this code:

      import argparse
      
      parser = argparse.ArgumentParser()
      group = parser.add_mutually_exclusive_group(required=True)
      group.add_argument("--foo", default="1")
      group.add_argument("--bar")
      args = parser.parse_args()
      print(args)
      

      When you explicitly pass --foo 1, it is treated as if no argument was passed:

      % python3 /tmp/bug.py --foo 1
      usage: bug.py [-h] (--foo FOO | --bar BAR)
      bug.py: error: one of the arguments --foo --bar is required
      

      I can't tell if this behavior is intentional, but it was surprising to me. It also seems to be somewhat based on the length of the default string. For example on my macOS machine if I change the default to longerstring it does not have this issue.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Feb 14, 2021
    3. keith commented on Feb 14, 2021

      keithmannequin
      MannequinAuthor

      Here's an example outside of argparse showing this is caused by the is comparison with interned string:

      import sys
      
      short_string = sys.argv[1]
      short_default = '1'
      long_string = sys.argv[2]
      long_default = 'not-interned'
      
      print(f"short comparisons: id1: {id(short_default)} id2: {id(short_string)}, eq: {short_default == short_string}, is: {short_default is short_string}")
      print(f"long comparisons: id1: {id(long_default)} id2: {id(long_string)}, eq: {long_default == long_string}, is: {long_default is long_string}")
      
      % ./python.exe /tmp/foo.py 1 not-interned
      short comparisons: id1: 4523386416 id2: 4523386416, eq: True, is: True
      long comparisons: id1: 4524440064 id2: 4523395296, eq: True, is: False
      
    4. changed the title [-]Explicit default required arguments with add_mutually_exclusive_group are rejected[/-] [+]Argparse: Explicit default required arguments with add_mutually_exclusive_group are rejected[/+] on Feb 15, 2021
    5. 4 remaining items

    6. self-assigned this
      on Apr 7, 2021
    7. paulj3 commented on Apr 7, 2021

      paulj3mannequin
      Mannequin

      An overlapping issue

      https://bugs.python.org/issue18943
      argparse: default args in mutually exclusive groups

      That issue shows that this problem arises with small integers as well (<257), which in cpython have unique ids. It's an implementation detail, pypy for example does not have this issue.

      The whole purpose of this extra default testing is to allow '?/*' positionals in mutually_exclusive_groups.

      The patch I proposed in 2013 is basically the same thing, except I called the new flag variable 'using_default'.

      We should review the discussion in that issue to see if it raises any additional issues or concerns.

    8. transferred this issue fromon Apr 10, 2022
    9. added
      type-bugAn unexpected behavior, bug, or error
      and removed on Aug 16, 2023
    10. serhiy-storchaka commented on Sep 25, 2024

      @serhiy-storchaka
      Member

      Closed as a duplicate of #63143.

    11. moved this from Bugs to Doc issues in Argparse issueson Sep 25, 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)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions