summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2021-11-12 03:53:23 (GMT)
committerGitHub <noreply@github.com>2021-11-12 03:53:23 (GMT)
commit807f839bbfd5805fb76eb3436c9252a0441296eb (patch)
tree18625893a4b2f7a568169872f606d616602e5ac1 /Lib/argparse.py
parent27b69e60daa7b191ee6bc76fb6d5fb7d793062ab (diff)
downloadcpython-807f839bbfd5805fb76eb3436c9252a0441296eb.zip
cpython-807f839bbfd5805fb76eb3436c9252a0441296eb.tar.gz
cpython-807f839bbfd5805fb76eb3436c9252a0441296eb.tar.bz2
bpo-45235: Revert an argparse bugfix that caused a regression (GH-29525)
* Revert "bpo-45235: Fix argparse overrides namespace with subparser defaults (GH-28420) (GH-28443)" This reverts commit a18d52269ab6071a605d6c72f6af585a4c533ca4.
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r--Lib/argparse.py13
1 files changed, 6 insertions, 7 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 4cc14f2..b44fa4f 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1210,8 +1210,7 @@ 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():
- if not hasattr(namespace, key):
- setattr(namespace, key, value)
+ setattr(namespace, key, value)
if arg_strings:
vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
@@ -1845,6 +1844,11 @@ 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:
@@ -1855,11 +1859,6 @@ 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)