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 unexpected behavior with argument group inside mutually exclusive group #82771

Closed
TimSanders mannequin opened this issue Oct 25, 2019 · 3 comments
Closed

argparse unexpected behavior with argument group inside mutually exclusive group #82771

TimSanders mannequin opened this issue Oct 25, 2019 · 3 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@TimSanders
Copy link
Mannequin

TimSanders mannequin commented Oct 25, 2019

BPO 38590
Nosy @iritkatriel
Superseder
  • bpo-22047: Deprecate unsupported nesting of argparse groups
  • 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 = <Date 2021-12-16.15:39:12.910>
    created_at = <Date 2019-10-25.18:13:04.742>
    labels = ['3.7', '3.8', 'type-bug', 'library', '3.9']
    title = 'argparse unexpected behavior with argument group inside mutually exclusive group'
    updated_at = <Date 2021-12-16.15:39:12.909>
    user = 'https://bugs.python.org/TimSanders'

    bugs.python.org fields:

    activity = <Date 2021-12-16.15:39:12.909>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-12-16.15:39:12.910>
    closer = 'iritkatriel'
    components = ['Library (Lib)']
    creation = <Date 2019-10-25.18:13:04.742>
    creator = 'Tim Sanders'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38590
    keywords = []
    message_count = 3.0
    messages = ['355370', '356004', '408716']
    nosy_count = 3.0
    nosy_names = ['paul.j3', 'Tim Sanders', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '22047'
    type = 'behavior'
    url = 'https://bugs.python.org/issue38590'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9']

    @TimSanders
    Copy link
    Mannequin Author

    TimSanders mannequin commented Oct 25, 2019

    argparse allows adding argument_groups inside of mutually_exclusive_groups, but the behavior is unintuitive and a bit buggy.

    Demo:

    import argparse
    
    parser = argparse.ArgumentParser()
    single_group = parser.add_argument_group(title='single_group')
    single_group.add_argument('--a', action='store_true')
    
    mutex_group = parser.add_mutually_exclusive_group()
    mutex_group.add_argument('--b', action='store_true')
    
    nested_group = mutex_group.add_argument_group(title='nested_group')
    nested_group.add_argument('--c', action='store_true')
    nested_group.add_argument('--d', action='store_true')
    
    parser.print_help()
    print(parser.parse_args())

    Example output:

    $ ~/test_args.py --a --b --c --d
    usage: test_args.py [-h] [--a] [--b] [--c] [--d]

    optional arguments:
    -h, --help show this help message and exit
    --b

    single_group:
    --a
    Namespace(a=True, b=True, c=True, d=True)

    Two issues I've noticed with this:

    • Arguments in the nested group show up in the usage string, but not in the help text. The nested arguments are still parsed and available in the result, as normal.
    • Arguments in the nested group are not mutually exclusive with any arguments in the containing mutually_exclusive_group.
      • I would expect:
        --b ok
        --c ok
        --d ok
        --b --c error
        --b --d error
        --c --d error*
        --b --c --d error

    *From a design perspective, it seems like argument_groups are meant to control display, and mutually_exclusive_groups are meant to control logic, so I think "error" makes sense here. I personally would like the ability to allow "--c --d", but I think that's a separate discussion and probably a new type of group.

    @TimSanders TimSanders mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 25, 2019
    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Nov 5, 2019

    With one exception, groups are not designed or intended to be nested. But by inheritance (from _ActionsContainer) nesting isn't blocked nor does it raise any errors.

    As you surmise, an ArgumentGroup, is used only for grouping the help lines. By default that are two groups, with 'optionals' and 'required' names (actually the later should be 'positional'). The rest are user defined. They don't affect parsing in any way.

    MutuallyExclusiveGroup is used in parsing. It also is used, to the extent possible, when formatting usage.

    If a MutuallyExclusiveGroup is nested inside another MutuallyExclusiveGroup the parsing effect is just one flat group. Usage can be messed up - that's been the subject of another bug/issue.

    A MutuallyExclusiveGroup may be put in an ArgumentGroup. This is a way of giving the exclusive group a title and/or description in the help.

    There is a bug/issue requesting some sort of inclusive group. I tried to develop such a patch, implementing nesting, complete logic control (not just the current xor). But the usage formatting needs a complete rewrite. Overall this is too complex of an addition. On StackOverFlow I tell people to implement their own post-parsing testing.

    @iritkatriel
    Copy link
    Member

    Nesting argument groups and mutually exclusive groups is now deprecated (see bpo-22047). Thank you for the bug report.

    @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.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant