Skip to content

argparse: creating an already defined subparsers does not raises an exception #59061

Closed as not planned

Description

@eacb
mannequin
BPO 14856
Nosy @bitdancer, @bradengroom, @iritkatriel
PRs
  • bpo-14856: Deprecate using add_parser() to overwrite an existing subparser #10159
  • bpo-14856: Deprecate using add_parser() to overwrite an existing subparser #10159
  • bpo-14856: Deprecate using add_parser() to overwrite an existing subparser #10159
  • Files
  • argparse_no_dup_subparsers.diff
  • 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 2012-05-19.12:27:50.832>
    labels = ['type-feature', 'library', '3.11']
    title = 'argparse: creating an already defined subparsers does not raises an exception'
    updated_at = <Date 2021-12-02.23:11:09.228>
    user = 'https://bugs.python.org/eacb'

    bugs.python.org fields:

    activity = <Date 2021-12-02.23:11:09.228>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2012-05-19.12:27:50.832>
    creator = 'eacb'
    dependencies = []
    files = ['25637']
    hgrepos = []
    issue_num = 14856
    keywords = ['patch']
    message_count = 5.0
    messages = ['161112', '161359', '166087', '328667', '407555']
    nosy_count = 5.0
    nosy_names = ['bethard', 'r.david.murray', 'eacb', 'bradengroom', 'iritkatriel']
    pr_nums = ['10159', '10159', '10159']
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue14856'
    versions = ['Python 3.11']

    Activity

    1. eacb commented on May 19, 2012

      eacbmannequin
      MannequinAuthor

      With this patch, it raises an ArgumentException, instead of overwriting previous subparser without notice.

      Regards.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on May 19, 2012
    3. bitdancer commented on May 22, 2012

      @bitdancer
      Member

      Thanks for the report and patch.

      However, since there is no del_parser method, I'm inclined to think that this is not a bug. That is, the ability to overwrite a previously defined subparser could be considered a feature.

      If Steven does consider it a bug, I think it can only be fixed in 3.3, since to do otherwise could needlessly break currently-working programs. If it is not a bug, add_parser's behavior when a parser already exist should be documented.

    4. bethard commented on Jul 21, 2012

      bethardmannequin
      Mannequin

      Yeah, overwriting the existing parser is probably not typically what the user intended.

      However, I could see someone doing this if, say, they had a parser designed by another module writer, and they wanted to use it but just change one of the sub-parsers or something like that. But a "set_parser" method would probably make more sense for this use case than "add_parser".

      I guess my recommendation is to add a "set_parser" method that replaces the old parser (like "add_parser" does now), and then issue deprecation warnings in "add_parser" if a parser with the given name is present (like the patch does, but with deprecation warnings instead of exceptions).

      We could then turn the warnings into errors in a few releases.

    5. bradengroom commented on Oct 28, 2018

      bradengroommannequin
      Mannequin
    6. iritkatriel commented on Dec 2, 2021

      @iritkatriel
      Member

      The open patch deprecates overriding the parser with add_parser, but it does not add the new set_parser method that Steven suggested.

    7. transferred this issue fromon Apr 10, 2022
    8. serhiy-storchaka commented on Oct 22, 2024

      @serhiy-storchaka
      Member

      Adding a parser with a name or an alias that matches an existing parser was forbidden in bpo-39716 (gh-83897).

    9. moved this from Features to Doc issues in Argparse issueson Oct 22, 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.11only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions