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

(cherry picked from commit 66b3922b97)

Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
Co-authored-by: rindeal <dev.rindeal@gmail.com>
This commit is contained in:
Serhiy Storchaka 2024-10-14 10:09:06 +03:00 committed by GitHub
parent 1279be610d
commit d3d306a9d6
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
@ -596,8 +595,7 @@ class HelpFormatter(object):
if hasattr(params[name], '__name__'):
params[name] = params[name].__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 self._get_help_string(action) % params
def _iter_indented_subactions(self, action):
@ -714,7 +712,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
@ -2595,8 +2593,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)