diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2021-11-12 03:53:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-12 03:53:23 (GMT) |
commit | 807f839bbfd5805fb76eb3436c9252a0441296eb (patch) | |
tree | 18625893a4b2f7a568169872f606d616602e5ac1 | |
parent | 27b69e60daa7b191ee6bc76fb6d5fb7d793062ab (diff) | |
download | cpython-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.
-rw-r--r-- | Lib/argparse.py | 13 | ||||
-rw-r--r-- | Lib/test/test_argparse.py | 6 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-11-11-13-03-17.bpo-45235.8ZbkHa.rst | 3 |
3 files changed, 9 insertions, 13 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) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 21732d1..f3edde3 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -3114,12 +3114,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 0000000..f73589c --- /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. |