cpython icon indicating copy to clipboard operation
cpython copied to clipboard

argparse.ArgumentParser does not raise on duplicated subparsers, even though it does on duplicated flags

Open 379dc349-3a10-424f-b9d2-a0104f092359 opened this issue 5 years ago • 4 comments

BPO 39716
Nosy @rhettinger, @anntzer
PRs
  • python/cpython#18605
  • 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-02-21.18:20:54.496>
    labels = ['library', '3.9']
    title = 'argparse.ArgumentParser does not raise on duplicated subparsers, even though it does on duplicated flags'
    updated_at = <Date 2020-02-22.11:04:34.546>
    user = 'https://github.com/anntzer'
    

    bugs.python.org fields:

    activity = <Date 2020-02-22.11:04:34.546>
    actor = 'Antony.Lee'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-02-21.18:20:54.496>
    creator = 'Antony.Lee'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 39716
    keywords = ['patch']
    message_count = 3.0
    messages = ['362421', '362428', '362458']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'paul.j3', 'Antony.Lee']
    pr_nums = ['18605']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue39716'
    versions = ['Python 3.9']
    

    If one tries to add twice the same flag to an ArgumentParser, one gets a helpful exception:

        from argparse import ArgumentParser
        p = ArgumentParser()
        p.add_argument("--foo")
        p.add_argument("--foo")
    

    results in

    argparse.ArgumentError: argument --foo: conflicting option string: --foo
    

    However, adding twice the same subparser raises no exception:

        from argparse import ArgumentParser
        p = ArgumentParser()
        sp = p.add_subparsers()
        sp.add_parser("foo")
        sp.add_parser("foo")
    

    even though the two subparsers shadow one another in the same way as two identical flags.

    Would you like to work-up a patch to fix this?

    rhettinger avatar Feb 21 '20 20:02 rhettinger

    Sure, https://github.com/python/cpython/pull/18605 it is.

    Please note that this issue is labeled as 3.9 and fix for it was submitted in February 2020, but the fix was actually released with 3.11

    ppolewicz avatar Nov 11 '22 13:11 ppolewicz

    @ppolewicz PRs are nearly always merged into main and only sometimes backported. Tracker version labels indicate the version(s) reported for, not necessarily the versions patched. Since we moved the repository to github, PR titles indicate the version(s) actually patched (with 'main' the default). The tracker move to github messed up merge reporting for old PRs. I repointed the PR for this issue to the gh number so it is listed immediately above.

    terryjreedy avatar Aug 16 '23 16:08 terryjreedy