classification
Title: argparse does not preserve namespace with subparser defaults
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: ALSchwalm, miss-islington, rhettinger
Priority: normal Keywords: patch

Created on 2021-09-17 14:58 by ALSchwalm, last changed 2021-09-18 06:48 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28420 merged ALSchwalm, 2021-09-17 15:18
PR 28442 merged miss-islington, 2021-09-18 04:20
PR 28443 merged miss-islington, 2021-09-18 04:20
Messages (4)
msg402060 - (view) Author: Adam Schwalm (ALSchwalm) * Date: 2021-09-17 14:58
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.
msg402120 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-18 04:20
New changeset a6e8db5e8e6780411db749d404715dbe021647c7 by Adam Schwalm in branch 'main':
bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420)
https://github.com/python/cpython/commit/a6e8db5e8e6780411db749d404715dbe021647c7
msg402121 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-18 06:47
New changeset 6e4101add568910409294554c8e863226a66bb64 by Miss Islington (bot) in branch '3.10':
bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420) (GH-28442)
https://github.com/python/cpython/commit/6e4101add568910409294554c8e863226a66bb64
msg402122 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-18 06:47
New changeset a18d52269ab6071a605d6c72f6af585a4c533ca4 by Miss Islington (bot) in branch '3.9':
bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420) (GH-28443)
https://github.com/python/cpython/commit/a18d52269ab6071a605d6c72f6af585a4c533ca4
History
Date User Action Args
2021-09-18 06:48:49rhettingersetstatus: open -> closed
assignee: rhettinger
resolution: fixed
stage: patch review -> resolved
2021-09-18 06:47:45rhettingersetmessages: + msg402122
2021-09-18 06:47:20rhettingersetmessages: + msg402121
2021-09-18 04:20:44miss-islingtonsetpull_requests: + pull_request26846
2021-09-18 04:20:40miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26845
2021-09-18 04:20:38rhettingersetnosy: + rhettinger
messages: + msg402120
2021-09-17 15:18:00ALSchwalmsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26832
2021-09-17 14:58:54ALSchwalmcreate