10000 [3.12] gh-86357: argparse: use str() consistently and explicitly to p… · python/cpython@21524ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 21524ee

Browse files
[3.12] gh-86357: argparse: use str() consistently and explicitly to print choices (GH-117766) (GH-125432)
(cherry picked from commit 66b3922) Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com> Co-authored-by: rindeal <dev.rindeal@gmail.com>
1 parent 20323bf commit 21524ee

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

Lib/argparse.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,7 @@ def _metavar_formatter(self, action, default_metavar):
588588
if action.metavar is not None:
589589
result = action.metavar
590590
elif action.choices is not None:
591-
choice_strs = [str(choice) for choice in action.choices]
592-
result = '{%s}' % ','.join(choice_strs)
591+
result = '{%s}' % ','.join(map(str, action.choices))
593592
else:
594593
result = default_metavar
595594

@@ -637,8 +636,7 @@ def _expand_help(self, action):
637636
if hasattr(params[name], '__name__'):
638637
params[name] = params[name].__name__
639638
if params.get('choices') is not None:
640-
choices_str = ', '.join([str(c) for c in params['choices']])
641-
params['choices'] = choices_str
639+
params['choices'] = ', '.join(map(str, params['choices']))
642640
return self._get_help_string(action) % params
643641

644642
def _iter_indented_subactions(self, action):
@@ -763,7 +761,7 @@ def _get_action_name(argument):
763761
elif argument.dest not in (None, SUPPRESS):
764762
return argument.dest
765763
elif argument.choices:
766-
return '{' + ','.join(argument.choices) + '}'
764+
return '{%s}' % ','.join(map(str, argument.choices))
767765
else:
768766
return None
769767

@@ -2600,8 +2598,8 @@ def _check_value(self, action, value):
26002598
if isinstance(choices, str):
26012599
choices = iter(choices)
26022600
if value not in choices:
2603-
args = {'value': value,
2604-
'choices': ', '.join(map(repr, action.choices))}
2601+
args = {'value': str(value),
2602+
'choices': ', '.join(map(str, action.choices))}
26052603
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
26062604
raise ArgumentError(action, msg % args)
26072605

Lib/test/test_argparse.py

Lines changed: 30 additions & 1 deletion
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
1920
from unittest import mock
2021

@@ -1021,6 +1022,34 @@ class TestDisallowLongAbbreviationAllowsShortGroupingPrefix(ParserTestCase):
10211022
]
10221023

10231024

1025+
class TestStrEnumChoices(TestCase):
1026+
class Color(StrEnum):
1027+
RED = "red"
1028+
GREEN = "green"
1029+
BLUE = "blue"
1030+
1031+
def test_parse_enum_value(self):
1032+
parser = argparse.ArgumentParser()
1033+
parser.add_argument('--color', choices=self.Color)
1034+
args = parser.parse_args(['--color', 'red'])
1035+
self.assertEqual(args.color, self.Color.RED)
1036+
1037+
def test_help_message_contains_enum_choices(self):
1038+
parser = argparse.ArgumentParser()
1039+
parser.add_argument('--color', choices=self.Color, help='Choose a color')
1040+
self.assertIn('[--color {red,green,blue}]', parser.format_usage())
1041+
self.assertIn(' --color {red,green,blue}', parser.format_help())
1042+
1043+
def test_invalid_enum_value_raises_error(self):
1044+
parser = argparse.ArgumentParser(exit_on_error=False)
1045+
parser.add_argument('--color', choices=self.Color)
1046+
self.assertRaisesRegex(
1047+
argparse.ArgumentError,
1048+
r"invalid choice: 'yellow' \(choose from red, green, blue\)",
1049+
parser.parse_args,
1050+
['--color', 'yellow'],
1051+
)
1052+
10241053
# ================
10251054
# Positional tests
10261055
# ================
@@ -2422,7 +2451,7 @@ def test_wrong_argument_subparsers_no_destination_error(self):
24222451
parser.parse_args(('baz',))
24232452
self.assertRegex(
24242453
excinfo.exception.stderr,
2425-
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
2454+
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from foo, bar\)\n$"
24262455
)
24272456

24282457
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