[issue45235] argparse does not preserve namespace with subparser defaults

2021-09-17 Thread Adam Schwalm


Change by Adam Schwalm :


--
keywords: +patch
pull_requests: +26832
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/28420

___
Python tracker 
<https://bugs.python.org/issue45235>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45235] argparse does not preserve namespace with subparser defaults

2021-09-17 Thread Adam Schwalm


New submission from Adam Schwalm :

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.

--
components: Library (Lib)
messages: 402060
nosy: ALSchwalm
priority: normal
severity: normal
status: open
title: argparse does not preserve namespace with subparser defaults
type: behavior
versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python 
3.9

___
Python tracker 
<https://bugs.python.org/issue45235>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com