summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorAdam Schwalm <adamschwalm@gmail.com>2021-09-18 04:20:31 (GMT)
committerGitHub <noreply@github.com>2021-09-18 04:20:31 (GMT)
commita6e8db5e8e6780411db749d404715dbe021647c7 (patch)
treedd2b82a52745e2f764788b613fc928670c01f026 /Lib/argparse.py
parentaf08f1ba40505bf1380c08b57ba4e0b8969a8358 (diff)
downloadcpython-a6e8db5e8e6780411db749d404715dbe021647c7.zip
cpython-a6e8db5e8e6780411db749d404715dbe021647c7.tar.gz
cpython-a6e8db5e8e6780411db749d404715dbe021647c7.tar.bz2
bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420)
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r--Lib/argparse.py13
1 files changed, 7 insertions, 6 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index d5a0afb..df98999 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1209,7 +1209,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, [])
@@ -1843,11 +1844,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:
@@ -1858,6 +1854,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)