Skip to content

Commit 4c79b5a

Browse files
Ensure quotes around suggestions
1 parent 7ebe924 commit 4c79b5a

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

Lib/argparse.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2757,8 +2757,14 @@ def _check_value(self, action, value):
27572757
choices = iter(choices)
27582758

27592759
if value not in choices:
2760+
def _format_choice(choice):
2761+
# For enum members, use repr of the value, not the enum itself
2762+
if hasattr(choice, 'value'):
2763+
return repr(choice.value)
2764+
return repr(choice)
2765+
27602766
args = {'value': str(value),
2761-
'choices': ', '.join(map(str, action.choices))}
2767+
'choices': ', '.join(map(_format_choice, action.choices))}
27622768
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
27632769

27642770
if self.suggest_on_error and isinstance(value, str):

Lib/test/test_argparse.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ def test_invalid_enum_value_raises_error(self):
11231123
parser.add_argument('--color', choices=self.Color)
11241124
self.assertRaisesRegex(
11251125
argparse.ArgumentError,
1126-
r"invalid choice: 'yellow' \(choose from red, green, blue\)",
1126+
r"invalid choice: 'yellow' \(choose from 'red', 'green', 'blue'\)",
11271127
parser.parse_args,
11281128
['--color', 'yellow'],
11291129
)
@@ -2392,7 +2392,7 @@ def test_wrong_argument_error_with_suggestions(self):
23922392
with self.assertRaises(ArgumentParserError) as excinfo:
23932393
parser.parse_args(('bazz',))
23942394
self.assertIn(
2395-
"error: argument foo: invalid choice: 'bazz', maybe you meant 'baz'? (choose from bar, baz)",
2395+
"error: argument foo: invalid choice: 'bazz', maybe you meant 'baz'? (choose from 'bar', 'baz')",
23962396
excinfo.exception.stderr
23972397
)
23982398

@@ -2402,7 +2402,7 @@ def test_wrong_argument_error_no_suggestions(self):
24022402
with self.assertRaises(ArgumentParserError) as excinfo:
24032403
parser.parse_args(('bazz',))
24042404
self.assertIn(
2405-
"error: argument foo: invalid choice: 'bazz' (choose from bar, baz)",
2405+
"error: argument foo: invalid choice: 'bazz' (choose from 'bar', 'baz')",
24062406
excinfo.exception.stderr,
24072407
)
24082408

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

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

@@ -2438,7 +2438,7 @@ def test_wrong_argument_with_suggestion_explicit(self):
24382438
parser.parse_args(('bazz',))
24392439
self.assertIn(
24402440
"error: argument foo: invalid choice: 'bazz', maybe you meant"
2441-
" 'baz'? (choose from bar, baz)",
2441+
" 'baz'? (choose from 'bar', 'baz')",
24422442
excinfo.exception.stderr,
24432443
)
24442444

@@ -2468,7 +2468,7 @@ def test_suggestions_choices_mixed_types(self):
24682468
with self.assertRaises(ArgumentParserError) as excinfo:
24692469
parser.parse_args(('3',))
24702470
self.assertIn(
2471-
"error: argument foo: invalid choice: '3' (choose from 1, 2)",
2471+
"error: argument foo: invalid choice: '3' (choose from 1, '2')",
24722472
excinfo.exception.stderr,
24732473
)
24742474

0 commit comments

Comments
 (0)