Skip to content

mutually exclusive group w/ all parameters argparse.SUPPRESS'd causes an assert #98666

Description

@kenyee

Bug report

state_group = parser.add_mutually_exclusive_group()
state_group.add_argument('--param1', nargs='?', const='default', metavar='NAME',
                         help=SUPPRESS)
state_group.add_argument('--param2', nargs='?', const='default', metavar='NAME',
                         help=SUPPRESS)

will cause an assert in
assert ' '.join(opt_parts) == opt_usage
of argparse.py

because the empty group [] is converted into two spaces
e.g.
[group1] [] [group 2]
gets converted to this by the opt_usage = format(optionals, groups) code above the assert:
[group1] [group 2]
and the two spaces don't match the single space of the assert

Your environment

  • CPython versions tested on: 3.10.4
  • Operating system and architecture: OSX, arm64

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 25, 2022
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 25, 2022
  3. hpaulj commented on Nov 4, 2022

    @hpaulj

    The usage formatting is known for being brittle, especially when working with the extra notation of mutually_exclusive_groups. This assert was intended to catch formatting errors that would cause problems if the usage has to be split onto several lines. Without getting into the details, I'm not surprised that a rarely used feature like SUPPRESS causes problems.

  4. added a commit that references this issue on May 28, 2023
    4336ac7
  5. added a commit that references this issue on May 7, 2024
  6. encukou commented on May 7, 2024

    @encukou
    Member

    I cannot reproduce with the above reproducer, which I expanded to:

    import argparse
    from argparse import SUPPRESS
    
    parser = argparse.ArgumentParser(prog='PROG')
    state_group = parser.add_mutually_exclusive_group()
    state_group.add_argument('--param1', nargs='?', const='default', metavar='NAME',
                             help=SUPPRESS)
    state_group.add_argument('--param2', nargs='?', const='default', metavar='NAME',
                             help=SUPPRESS)
    parser.parse_args(['--help'])

    This is giving me the output:

    usage: PROG [-h]
    
    optional arguments:
      -h, --help  show this help message and exit
    

    on Python versions 3.6 to 3.13.

    If you have a complete example of failing behavior (even on an old version of Python), please share it here.

    I think this problem was fixed in #105039; I'll close the issue.

  7. moved this from Bugs to Doc issues in Argparse issueson May 7, 2024
  8. added a commit that references this issue on May 8, 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

    stdlibStandard 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