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