Skip to content

Commit a4334af

Browse files
rindeallocalhost
authored and
localhost
committed
argparse: use str() consistently and explicitly to print choices
Signed-off-by: Jan Chren (rindeal) <[email protected]>
1 parent 73906d5 commit a4334af

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

Lib/argparse.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,7 @@ def _metavar_formatter(self, action, default_metavar):
584584
if action.metavar is not None:
585585
result = action.metavar
586586
elif action.choices is not None:
587-
choice_strs = [str(choice) for choice in action.choices]
588-
result = '{%s}' % ','.join(choice_strs)
587+
result = '{%s}' % ','.join(map(str, action.choices))
589588
else:
590589
result = default_metavar
591590

@@ -633,8 +632,7 @@ def _expand_help(self, action):
633632
if hasattr(params[name], '__name__'):
634633
params[name] = params[name].__name__
635634
if params.get('choices') is not None:
636-
choices_str = ', '.join([str(c) for c in params['choices']])
637-
params['choices'] = choices_str
635+
params['choices'] = ', '.join(map(str, params['choices']))
638636
return self._get_help_string(action) % params
639637

640638
def _iter_indented_subactions(self, action):
@@ -743,7 +741,7 @@ def _get_action_name(argument):
743741
elif argument.dest not in (None, SUPPRESS):
744742
return argument.dest
745743
elif argument.choices:
746-
return '{' + ','.join(argument.choices) + '}'
744+
return '{%s}' % ','.join(map(str, argument.choices))
747745
else:
748746
return None
749747

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

0 commit comments

Comments
 (0)