8000 argparse: use str() consistently and explicitly to print choices · python/cpython@48f6b8c · GitHub
[go: up one dir, main page]

Skip to content

Commit 48f6b8c

Browse files
rindeallocalhost
authored and
localhost
committed
argparse: use str() consistently and explicitly to print choices
Fixes: #118839 Signed-off-by: Jan Chren (rindeal) <dev.rindeal@gmail.com>
1 parent 73906d5 commit 48f6b8c

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
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

Lib/test/test_argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2361,7 +2361,7 @@ def test_wrong_argument_subparsers_no_destination_error(self):
23612361
parser.parse_args(('baz',))
23622362
self.assertRegex(
23632363
excinfo.exception.stderr,
2364-
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
2364+
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from foo, bar\)\n$"
23652365
)
23662366

23672367
def test_optional_subparsers(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Always use :func:`str()` to print ``choices`` in :mod:`argparse`.

0 commit comments

Comments
 (0)
0