mirror of
https://github.com/python/cpython.git
synced 2025-08-31 22:18:28 +00:00
bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420) (GH-28443)
This commit is contained in:
parent
6e151ff6fc
commit
a18d52269a
3 changed files with 15 additions and 6 deletions
|
@ -1208,7 +1208,8 @@ class _SubParsersAction(Action):
|
|||
# namespace for the relevant parts.
|
||||
subnamespace, arg_strings = parser.parse_known_args(arg_strings, None)
|
||||
for key, value in vars(subnamespace).items():
|
||||
setattr(namespace, key, value)
|
||||
if not hasattr(namespace, key):
|
||||
setattr(namespace, key, value)
|
||||
|
||||
if arg_strings:
|
||||
vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
|
||||
|
@ -1842,11 +1843,6 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
if action.default is not SUPPRESS:
|
||||
setattr(namespace, action.dest, action.default)
|
||||
|
||||
# add any parser defaults that aren't present
|
||||
for dest in self._defaults:
|
||||
if not hasattr(namespace, dest):
|
||||
setattr(namespace, dest, self._defaults[dest])
|
||||
|
||||
# parse the arguments and exit if there are any errors
|
||||
if self.exit_on_error:
|
||||
try:
|
||||
|
@ -1857,6 +1853,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
else:
|
||||
namespace, args = self._parse_known_args(args, namespace)
|
||||
|
||||
# add any parser defaults that aren't present
|
||||
for dest in self._defaults:
|
||||
if not hasattr(namespace, dest):
|
||||
setattr(namespace, dest, self._defaults[dest])
|
||||
|
||||
if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
|
||||
args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR))
|
||||
delattr(namespace, _UNRECOGNIZED_ARGS_ATTR)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue