gh-86357: argparse: use str() consistently and explicitly to print choices (GH-117766)

Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
This commit is contained in:
rindeal 2024-10-14 06:36:53 +00:00 committed by GitHub
parent cfc27bc50f
commit 66b3922b97
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 8 deletions

View file

@ -547,8 +547,7 @@ class HelpFormatter(object):
if action.metavar is not None:
result = action.metavar
elif action.choices is not None:
choice_strs = [str(choice) for choice in action.choices]
result = '{%s}' % ','.join(choice_strs)
result = '{%s}' % ','.join(map(str, action.choices))
else:
result = default_metavar
@ -599,8 +598,7 @@ class HelpFormatter(object):
elif hasattr(value, '__name__'):
params[name] = value.__name__
if params.get('choices') is not None:
choices_str = ', '.join([str(c) for c in params['choices']])
params['choices'] = choices_str
params['choices'] = ', '.join(map(str, params['choices']))
return help_string % params
def _iter_indented_subactions(self, action):
@ -717,7 +715,7 @@ def _get_action_name(argument):
elif argument.dest not in (None, SUPPRESS):
return argument.dest
elif argument.choices:
return '{' + ','.join(argument.choices) + '}'
return '{%s}' % ','.join(map(str, argument.choices))
else:
return None
@ -2607,8 +2605,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
if isinstance(choices, str):
choices = iter(choices)
if value not in choices:
args = {'value': value,
'choices': ', '.join(map(repr, action.choices))}
args = {'value': str(value),
'choices': ', '.join(map(str, action.choices))}
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
raise ArgumentError(action, msg % args)