Skip to content

argparse does not preserve namespace with subparser defaults #89398

Open

Description

@ALSchwalm
mannequin
BPO 45235
Nosy @rhettinger, @ambv, @pablogsal, @miss-islington, @frostming, @RhinosF1, @ALSchwalm, @dgw, @lwalejko
PRs
  • bpo-45235: Fix argparse overrides namespace with subparser defaults #28420
  • [3.10] bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420) #28442
  • [3.9] bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420) #28443
  • bpo-45235: Revert an argparse bugfix that caused a regression #29525
  • [3.10] bpo-45235: Revert an argparse bugfix that caused a regression (GH-29525) #29530
  • [3.9] bpo-45235: Revert an argparse bugfix that caused a regression (GH-29525) #29531
  • bpo-45235: Fix argparse namespace overridden by subparsers default #29574
  • gh-89398: Fix argparse namespace overridden by subparsers default #30219
  • 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-09-17.14:58:54.103>
    labels = ['type-bug', '3.8', '3.9', '3.10', '3.11', '3.7', 'library']
    title = 'argparse does not preserve namespace with subparser defaults'
    updated_at = <Date 2021-12-22.19:20:37.779>
    user = 'https://github.com/ALSchwalm'

    bugs.python.org fields:

    activity = <Date 2021-12-22.19:20:37.779>
    actor = 'gvanrossum'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-09-17.14:58:54.103>
    creator = 'ALSchwalm'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45235
    keywords = ['patch']
    message_count = 23.0
    messages = ['402060', '402120', '402121', '402122', '404990', '404991', '405007', '405129', '406052', '406079', '406121', '406123', '406128', '406171', '406172', '406175', '406186', '406231', '406232', '406233', '406571', '406814', '408991']
    nosy_count = 10.0
    nosy_names = ['rhettinger', 'lukasz.langa', 'paul.j3', 'pablogsal', 'miss-islington', 'Frost Ming', 'RhinosF1', 'ALSchwalm', 'dgw', 'lwalejko']
    pr_nums = ['28420', '28442', '28443', '29525', '29530', '29531', '29574', '30219']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45235'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. ALSchwalm commented on Sep 17, 2021

      ALSchwalmmannequin
      MannequinAuthor

      The following snippet demonstrates the problem. If a subparser flag has a default set, argparse will override the existing value in the provided 'namespace' if the flag does not appear (e.g., if the default is used):

          import argparse
          parser = argparse.ArgumentParser()
          sub = parser.add_subparsers()
          example_subparser = sub.add_parser("example")
          example_subparser.add_argument("--flag", default=10)
          print(parser.parse_args(["example"], argparse.Namespace(flag=20)))

      This should return 'Namespace(flag=20)' because 'flag' already exists in the namespace, but instead it returns 'Namespace(flag=10)'. This intended behavior is described and demonstrated in the second example here: https://docs.python.org/3/library/argparse.html#default

      Lib's behavior is correct for the non-subparser cause.

    2. added
      3.7 (EOL)
      3.8 (EOL)
      3.9 (EOL)
      3.10 (EOL)
      3.11
      stdlib
      type-bug
      on Sep 17, 2021
    3. 27 remaining items

    4. removed their assignment
      on Nov 19, 2021
    5. FrostMing commented on Nov 23, 2021

      FrostMingmannequin
      Mannequin

      Hi, I noticed this bug because of the regression of Python 3.9.8. And I proposed a better approach in PR 29574.

      Maybe the folks here can have a look. Thanks

      • Frost
    6. FrostMing commented on Dec 21, 2021

      FrostMingmannequin
      Mannequin

      Per the review comments of @jiasli, I worked out a second PR to fix this issue.

      This fix has less side-effect and better backward compatibility. I will leave the two PRs open. Any feedback is welcome.

      • Frost
    7. transferred this issue fromon Apr 10, 2022
    8. added
      3.12
      and removed
      3.11
      3.10 (EOL)
      3.9 (EOL)
      3.8 (EOL)
      3.7 (EOL)
      on Jan 7, 2023
    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.12stdlibtype-bug

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions