summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-11-12 10:44:25 -0800
committerGitHub <noreply@github.com>2021-11-12 12:44:25 -0600
commit587ff7f50bcbfd8346c6d5db459a1628a350c04d (patch)
tree6719e39328cb8ce3d490ca6532797bb64ebf14fb
parent8b6a474071bcc88ec3453e16f079181e551513c3 (diff)
downloadcpython-git-587ff7f50bcbfd8346c6d5db459a1628a350c04d.tar.gz
bpo-45235: Revert an argparse bugfix that caused a regression (GH-29525) (GH-29531)
-rw-r--r--Lib/argparse.py13
-rw-r--r--Lib/test/test_argparse.py6
-rw-r--r--Misc/NEWS.d/next/Library/2021-11-11-13-03-17.bpo-45235.8ZbkHa.rst3
3 files changed, 9 insertions, 13 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index cb5345d555..7c28547f3e 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1209,8 +1209,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, [])
@@ -1844,6 +1843,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:
@@ -1854,11 +1858,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)
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index c3c3a75faf..d79ac5a465 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -3095,12 +3095,6 @@ class TestSetDefaults(TestCase):
xparser.set_defaults(foo=2)
self.assertEqual(NS(foo=2), parser.parse_args(['X']))
- def test_set_defaults_on_subparser_with_namespace(self):
- parser = argparse.ArgumentParser()
- xparser = parser.add_subparsers().add_parser('X')
- xparser.set_defaults(foo=1)
- self.assertEqual(NS(foo=2), parser.parse_args(['X'], NS(foo=2)))
-
def test_set_defaults_same_as_add_argument(self):
parser = ErrorRaisingArgumentParser()
parser.set_defaults(w='W', x='X', y='Y', z='Z')
diff --git a/Misc/NEWS.d/next/Library/2021-11-11-13-03-17.bpo-45235.8ZbkHa.rst b/Misc/NEWS.d/next/Library/2021-11-11-13-03-17.bpo-45235.8ZbkHa.rst
new file mode 100644
index 0000000000..f73589ccc8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-11-11-13-03-17.bpo-45235.8ZbkHa.rst
@@ -0,0 +1,3 @@
+Reverted an argparse bugfix that caused regression in the handling of
+default arguments for subparsers. This prevented leaf level arguments from
+taking precedence over root level arguments.