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

Skip to content

Commit d7b5546

Browse files
committed
argparse: use str() consistently and explicitly to print choices
Fixes: #86357 Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
1 parent c58c572 commit d7b5546

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-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: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import argparse
1616
import warnings
1717

18+
from enum import StrEnum
1819
from test.support import os_helper, captured_stderr
1920
from unittest import mock
2021

@@ -952,6 +953,35 @@ class TestDisallowLongAbbreviationAllowsShortGroupingPrefix(ParserTestCase):
952953
]
953954

954955

956+
class TestStrEnumChoices(TestCase):
957+
class Color(StrEnum):
958+
RED = "red"
959+
GREEN = "green"
960+
BLUE = "blue"
961+
962+
def test_metavar_formatter_with_strenum(self):
963+
parser = argparse.ArgumentParser()
964+
parser.add_argument('--color', choices=self.Color)
965+
args = parser.parse_args(['--color', 'red'])
966+
self.assertEqual(args.color, self.Color.RED)
967+
968+
def test_expand_help_with_strenum(self):
969+
parser = argparse.ArgumentParser()
970+
parser.add_argument('--color', choices=self.Color, help='Choose a color')
971+
help_output = parser.format_help()
972+
self.assertIn('[--color {red,green,blue}]', help_output)
973+
self.assertIn(' --color {red,green,blue}', help_output)
974+
975+
def test_check_value_with_strenum(self):
976+
parser = argparse.ArgumentParser(exit_on_error=False)
977+
parser.add_argument('--color', choices=self.Color)
978+
self.assertRaisesRegex(
979+
argparse.ArgumentError,
980+
r"invalid choice: yellow \(choose from red, green, blue\)",
981+
parser.parse_args,
982+
['--color', 'yellow'],
983+
)
984+
955985
# ================
956986
# Positional tests
957987
# ================
@@ -2399,7 +2429,7 @@ def test_wrong_argument_subparsers_no_destination_error(self):
23992429
parser.parse_args(('baz',))
24002430
self.assertRegex(
24012431
excinfo.exception.stderr,
2402-
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
2432+
r"error: argument {foo,bar}: invalid choice: baz \(choose from foo, bar\)\n$"
24032433
)
24042434

24052435
def test_optional_subparsers(self):
@@ -6061,7 +6091,7 @@ def test_subparser(self):
60616091
args = parser.parse_args(['x', '--', 'run', '--', 'a', '--', 'b'])
60626092
self.assertEqual(NS(foo='x', f=None, bar=['a', '--', 'b']), args)
60636093
self.assertRaisesRegex(argparse.ArgumentError,
6064-
"invalid choice: '--'",
6094+
"invalid choice: --",
60656095
parser.parse_args, ['--', 'x', '--', 'run', 'a', 'b'])
60666096

60676097
def test_subparser_after_multiple_argument_option(self):
@@ -6075,7 +6105,7 @@ def test_subparser_after_multiple_argument_option(self):
60756105
args = parser.parse_args(['--foo', 'x', 'y', '--', 'run', 'a', 'b', '-f', 'c'])
60766106
self.assertEqual(NS(foo=['x', 'y'], f='c', bar=['a', 'b']), args)
60776107
self.assertRaisesRegex(argparse.ArgumentError,
6078-
"invalid choice: '--'",
6108+
"invalid choice: --",
60796109
parser.parse_args, ['--foo', 'x', '--', '--', 'run', 'a', 'b'])
60806110

60816111

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