mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[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:
parent
1279be610d
commit
d3d306a9d6
3 changed files with 36 additions and 8 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue