Skip to content

Commit aea8322

Browse files
committed
argparse: use str() consistently and explicitly to print choices
Fixes: #118839 Signed-off-by: Jan Chren ~rindeal <[email protected]>
1 parent c58c572 commit aea8322

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

Lib/argparse.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,7 @@ def _metavar_formatter(self, action, default_metavar):
548548
if action.metavar is not None:
549549
result = action.metavar
550550
elif action.choices is not None:
551-
choice_strs = [str(choice) for choice in action.choices]
552-
result = '{%s}' % ','.join(choice_strs)
551+
result = '{%s}' % ','.join(map(str, action.choices))
553552
else:
554553
result = default_metavar
555554

@@ -597,8 +596,7 @@ def _expand_help(self, action):
597596
if hasattr(params[name], '__name__'):
598597
params[name] = params[name].__name__
599598
if params.get('choices') is not None:
600-
choices_str = ', '.join([str(c) for c in params['choices']])
601-
params['choices'] = choices_str
599+
params['choices'] = ', '.join(map(str, params['choices']))
602600
return self._get_help_string(action) % params
603601

604602
def _iter_indented_subactions(self, action):
@@ -707,7 +705,7 @@ def _get_action_name(argument):
707705
elif argument.dest not in (None, SUPPRESS):
708706
return argument.dest
709707
elif argument.choices:
710-
return '{' + ','.join(argument.choices) + '}'
708+
return '{%s}' % ','.join(map(str, argument.choices))
711709
else:
712710
return None
713711

@@ -2556,8 +2554,8 @@ def _check_value(self, action, value):
25562554
# converted value must be one of the choices (if specified)
25572555
if action.choices is not None and value not in action.choices:
25582556
args = {'value': value,
2559-
'choices': ', '.join(map(repr, action.choices))}
2560-
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
2557+
'choices': ', '.join(map(str, action.choices))}
2558+
msg = _('invalid choice: %(value)s (choose from %(choices)s)')
25612559
raise ArgumentError(action, msg % args)
25622560

25632561
# =======================

Lib/test/test_argparse.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,7 +2399,7 @@ def test_wrong_argument_subparsers_no_destination_error(self):
23992399
parser.parse_args(('baz',))
24002400
self.assertRegex(
24012401
excinfo.exception.stderr,
2402-
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
2402+
r"error: argument {foo,bar}: invalid choice: baz \(choose from foo, bar\)\n$"
24032403
)
24042404

24052405
def test_optional_subparsers(self):
@@ -6061,7 +6061,7 @@ def test_subparser(self):
60616061
args = parser.parse_args(['x', '--', 'run', '--', 'a', '--', 'b'])
60626062
self.assertEqual(NS(foo='x', f=None, bar=['a', '--', 'b']), args)
60636063
self.assertRaisesRegex(argparse.ArgumentError,
6064-
"invalid choice: '--'",
6064+
"invalid choice: --",
60656065
parser.parse_args, ['--', 'x', '--', 'run', 'a', 'b'])
60666066

60676067
def test_subparser_after_multiple_argument_option(self):
@@ -6075,7 +6075,7 @@ def test_subparser_after_multiple_argument_option(self):
60756075
args = parser.parse_args(['--foo', 'x', 'y', '--', 'run', 'a', 'b', '-f', 'c'])
60766076
self.assertEqual(NS(foo=['x', 'y'], f='c', bar=['a', 'b']), args)
60776077
self.assertRaisesRegex(argparse.ArgumentError,
6078-
"invalid choice: '--'",
6078+
"invalid choice: --",
60796079
parser.parse_args, ['--foo', 'x', '--', '--', 'run', 'a', 'b'])
60806080

60816081

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)