Skip to content

bpo-43220: Accept explicit default args in required groups #24526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,12 +1981,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:
Expand Down Expand Up @@ -2488,6 +2488,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('--')
Expand All @@ -2500,6 +2501,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)
Expand All @@ -2510,6 +2512,7 @@ def _get_values(self, action, arg_strings):
not action.option_strings):
if action.default is not None:
value = action.default
explicit = False
self._check_value(action, value)
else:
# since arg_strings is always [] at this point
Expand Down Expand Up @@ -2542,7 +2545,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)
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,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
# =================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -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.