Skip to content

Commit

Permalink
argparse: use str() consistently and explicitly to print choices
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Chren (rindeal) <dev.rindeal@gmail.com>
  • Loading branch information
rindeal authored and localhost committed Apr 11, 2024
1 parent 73906d5 commit a4334af
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,7 @@ def _metavar_formatter(self, action, default_metavar):
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

Expand Down Expand Up @@ -633,8 +632,7 @@ def _expand_help(self, action):
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):
Expand Down Expand Up @@ -743,7 +741,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

Expand Down Expand Up @@ -2613,7 +2611,7 @@ def _check_value(self, action, value):
# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
args = {'value': value,
'choices': ', '.join(map(repr, action.choices))}
'choices': ', '.join(map(str, action.choices))}
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
raise ArgumentError(action, msg % args)

Expand Down

0 comments on commit a4334af

Please sign in to comment.