From 85260233a9fb97fd71154a70b3449f4661b432e1 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Sat, 13 Feb 2021 22:13:11 -0800 Subject: [PATCH 1/2] bpo-43220: Accept explicit default args in required groups Previously if you had a default argument on a required mutually exclusive group and the default was explicitly passed, it was treated as if no value was passed. Now it accepts that value. --- Lib/argparse.py | 9 ++++++--- Lib/test/test_argparse.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 8a12dea7668799..02f97f05c944a0 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1909,12 +1909,12 @@ def _parse_known_args(self, arg_strings, namespace): def take_action(action, argument_strings, option_string=None): seen_actions.add(action) - argument_values = self._get_values(action, argument_strings) + argument_values, explicit = self._get_values(action, argument_strings) # error if this argument is not allowed with other previously # seen arguments, assuming that actions that use the default # value don't really count as "present" - if argument_values is not action.default: + if argument_values is not action.default or explicit: seen_non_default_actions.add(action) for conflict_action in action_conflicts.get(action, []): if conflict_action in seen_non_default_actions: @@ -2411,6 +2411,7 @@ def parse_known_intermixed_args(self, args=None, namespace=None): # ======================== def _get_values(self, action, arg_strings): # for everything but PARSER, REMAINDER args, strip out first '--' + explicit = True if action.nargs not in [PARSER, REMAINDER]: try: arg_strings.remove('--') @@ -2423,6 +2424,7 @@ def _get_values(self, action, arg_strings): value = action.const else: value = action.default + explicit = False if isinstance(value, str): value = self._get_value(action, value) self._check_value(action, value) @@ -2433,6 +2435,7 @@ def _get_values(self, action, arg_strings): not action.option_strings): if action.default is not None: value = action.default + explicit = False else: value = arg_strings self._check_value(action, value) @@ -2463,7 +2466,7 @@ def _get_values(self, action, arg_strings): self._check_value(action, v) # return the converted value - return value + return value, explicit def _get_value(self, action, arg_string): type_func = self._registry_get('type', action.type, action.type) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index ec9711e4f6a472..8876a7e808d46b 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2924,6 +2924,23 @@ def get_parser(self, required): test_successes_when_not_required = None test_successes_when_required = None +class TestMutuallyExclusiveRequiredDefault(TestCase): + + def test_default_arg_passed(self): + parser = ErrorRaisingArgumentParser() + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument('--foo', default='1') + group.add_argument('--bar') + self.assertEqual(NS(foo='1', bar=None), + parser.parse_args(['--foo', '1'])) + + def test_nothing_passed(self): + parser = ErrorRaisingArgumentParser() + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument('--foo', default='1') + group.add_argument('--bar') + self.assertRaises(ArgumentParserError, parser.parse_args, []) + # ================================================= # Mutually exclusive group in parent parser tests # ================================================= From 041e2e3698e6905c3dcdfae2437aac02b5288db5 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 16 Feb 2021 18:13:01 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-02-16-18-13-00.bpo-43220.XfDZu0.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-02-16-18-13-00.bpo-43220.XfDZu0.rst diff --git a/Misc/NEWS.d/next/Library/2021-02-16-18-13-00.bpo-43220.XfDZu0.rst b/Misc/NEWS.d/next/Library/2021-02-16-18-13-00.bpo-43220.XfDZu0.rst new file mode 100644 index 00000000000000..8d1863abfb68f9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-02-16-18-13-00.bpo-43220.XfDZu0.rst @@ -0,0 +1 @@ +Accept explicitly passed default arguments in required argparse groups. Previously short strings that were used as default values in argparse groups would be rejected if they were passed with the same value. Now they are accepted. \ No newline at end of file