Skip to content
Merged
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2758,7 +2758,7 @@ def _check_value(self, action, value):

if value not in choices:
args = {'value': str(value),
'choices': ', '.join(map(str, action.choices))}
'choices': ', '.join(repr(str(choice)) for choice in action.choices)}
msg = _('invalid choice: %(value)r (choose from %(choices)s)')

if self.suggest_on_error and isinstance(value, str):
Expand Down
16 changes: 8 additions & 8 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ def test_invalid_enum_value_raises_error(self):
parser.add_argument('--color', choices=self.Color)
self.assertRaisesRegex(
argparse.ArgumentError,
r"invalid choice: 'yellow' \(choose from red, green, blue\)",
r"invalid choice: 'yellow' \(choose from 'red', 'green', 'blue'\)",
parser.parse_args,
['--color', 'yellow'],
)
Expand Down Expand Up @@ -2392,7 +2392,7 @@ def test_wrong_argument_error_with_suggestions(self):
with self.assertRaises(ArgumentParserError) as excinfo:
parser.parse_args(('bazz',))
self.assertIn(
"error: argument foo: invalid choice: 'bazz', maybe you meant 'baz'? (choose from bar, baz)",
"error: argument foo: invalid choice: 'bazz', maybe you meant 'baz'? (choose from 'bar', 'baz')",
excinfo.exception.stderr
)

Expand All @@ -2402,7 +2402,7 @@ def test_wrong_argument_error_no_suggestions(self):
with self.assertRaises(ArgumentParserError) as excinfo:
parser.parse_args(('bazz',))
self.assertIn(
"error: argument foo: invalid choice: 'bazz' (choose from bar, baz)",
"error: argument foo: invalid choice: 'bazz' (choose from 'bar', 'baz')",
excinfo.exception.stderr,
)

Expand All @@ -2415,7 +2415,7 @@ def test_wrong_argument_subparsers_with_suggestions(self):
parser.parse_args(('baz',))
self.assertIn(
"error: argument {foo,bar}: invalid choice: 'baz', maybe you meant"
" 'bar'? (choose from foo, bar)",
" 'bar'? (choose from 'foo', 'bar')",
excinfo.exception.stderr,
)

Expand All @@ -2427,7 +2427,7 @@ def test_wrong_argument_subparsers_no_suggestions(self):
with self.assertRaises(ArgumentParserError) as excinfo:
parser.parse_args(('baz',))
self.assertIn(
"error: argument {foo,bar}: invalid choice: 'baz' (choose from foo, bar)",
"error: argument {foo,bar}: invalid choice: 'baz' (choose from 'foo', 'bar')",
excinfo.exception.stderr,
)

Expand All @@ -2438,7 +2438,7 @@ def test_wrong_argument_with_suggestion_explicit(self):
parser.parse_args(('bazz',))
self.assertIn(
"error: argument foo: invalid choice: 'bazz', maybe you meant"
" 'baz'? (choose from bar, baz)",
" 'baz'? (choose from 'bar', 'baz')",
excinfo.exception.stderr,
)

Expand All @@ -2458,7 +2458,7 @@ def test_suggestions_choices_int(self):
with self.assertRaises(ArgumentParserError) as excinfo:
parser.parse_args(('3',))
self.assertIn(
"error: argument foo: invalid choice: '3' (choose from 1, 2)",
"error: argument foo: invalid choice: '3' (choose from '1', '2')",
excinfo.exception.stderr,
)

Expand All @@ -2468,7 +2468,7 @@ def test_suggestions_choices_mixed_types(self):
with self.assertRaises(ArgumentParserError) as excinfo:
parser.parse_args(('3',))
self.assertIn(
"error: argument foo: invalid choice: '3' (choose from 1, 2)",
"error: argument foo: invalid choice: '3' (choose from '1', '2')",
excinfo.exception.stderr,
)

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def test_main_with_time_unit(self):
seconds_per_increment=0.003, switches=["-u", "parsec"]
)
self.assertIn(
"choose from nsec, usec, msec, sec", error_stringio.getvalue()
"choose from 'nsec', 'usec', 'msec', 'sec'", error_stringio.getvalue()
)

def test_main_exception(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Restore quoting of choices in :mod:`argparse` error messages for improved clarity and consistency with documentation.

Loading