Skip to content

Commit a458589

Browse files
[3.14] GH-130750: Restore quoting of choices in argparse error messag… (#149385)
[3.14] GH-130750: Restore quoting of choices in argparse error messages to match documentation and improve clarity (GH-144983) (cherry picked from commit 53a7f76)
1 parent 316f626 commit a458589

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2678,7 +2678,7 @@ def _check_value(self, action, value):
26782678

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

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

Lib/test/test_argparse.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ def test_invalid_enum_value_raises_error(self):
10451045
parser.add_argument('--color', choices=self.Color)
10461046
self.assertRaisesRegex(
10471047
argparse.ArgumentError,
1048-
r"invalid choice: 'yellow' \(choose from red, green, blue\)",
1048+
r"invalid choice: 'yellow' \(choose from 'red', 'green', 'blue'\)",
10491049
parser.parse_args,
10501050
['--color', 'yellow'],
10511051
)
@@ -2313,7 +2313,7 @@ def test_wrong_argument_error_with_suggestions(self):
23132313
with self.assertRaises(ArgumentParserError) as excinfo:
23142314
parser.parse_args(('bazz',))
23152315
self.assertIn(
2316-
"error: argument foo: invalid choice: 'bazz', maybe you meant 'baz'? (choose from bar, baz)",
2316+
"error: argument foo: invalid choice: 'bazz', maybe you meant 'baz'? (choose from 'bar', 'baz')",
23172317
excinfo.exception.stderr
23182318
)
23192319

@@ -2323,7 +2323,7 @@ def test_wrong_argument_error_no_suggestions(self):
23232323
with self.assertRaises(ArgumentParserError) as excinfo:
23242324
parser.parse_args(('bazz',))
23252325
self.assertIn(
2326-
"error: argument foo: invalid choice: 'bazz' (choose from bar, baz)",
2326+
"error: argument foo: invalid choice: 'bazz' (choose from 'bar', 'baz')",
23272327
excinfo.exception.stderr,
23282328
)
23292329

@@ -2336,7 +2336,7 @@ def test_wrong_argument_subparsers_with_suggestions(self):
23362336
parser.parse_args(('baz',))
23372337
self.assertIn(
23382338
"error: argument {foo,bar}: invalid choice: 'baz', maybe you meant"
2339-
" 'bar'? (choose from foo, bar)",
2339+
" 'bar'? (choose from 'foo', 'bar')",
23402340
excinfo.exception.stderr,
23412341
)
23422342

@@ -2348,7 +2348,7 @@ def test_wrong_argument_subparsers_no_suggestions(self):
23482348
with self.assertRaises(ArgumentParserError) as excinfo:
23492349
parser.parse_args(('baz',))
23502350
self.assertIn(
2351-
"error: argument {foo,bar}: invalid choice: 'baz' (choose from foo, bar)",
2351+
"error: argument {foo,bar}: invalid choice: 'baz' (choose from 'foo', 'bar')",
23522352
excinfo.exception.stderr,
23532353
)
23542354

@@ -2358,7 +2358,7 @@ def test_wrong_argument_no_suggestion_implicit(self):
23582358
with self.assertRaises(ArgumentParserError) as excinfo:
23592359
parser.parse_args(('bazz',))
23602360
self.assertIn(
2361-
"error: argument foo: invalid choice: 'bazz' (choose from bar, baz)",
2361+
"error: argument foo: invalid choice: 'bazz' (choose from 'bar', 'baz')",
23622362
excinfo.exception.stderr,
23632363
)
23642364

@@ -2378,7 +2378,7 @@ def test_suggestions_choices_int(self):
23782378
with self.assertRaises(ArgumentParserError) as excinfo:
23792379
parser.parse_args(('3',))
23802380
self.assertIn(
2381-
"error: argument foo: invalid choice: '3' (choose from 1, 2)",
2381+
"error: argument foo: invalid choice: '3' (choose from '1', '2')",
23822382
excinfo.exception.stderr,
23832383
)
23842384

@@ -2388,7 +2388,7 @@ def test_suggestions_choices_mixed_types(self):
23882388
with self.assertRaises(ArgumentParserError) as excinfo:
23892389
parser.parse_args(('3',))
23902390
self.assertIn(
2391-
"error: argument foo: invalid choice: '3' (choose from 1, 2)",
2391+
"error: argument foo: invalid choice: '3' (choose from '1', '2')",
23922392
excinfo.exception.stderr,
23932393
)
23942394

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Restore quoting of choices in :mod:`argparse` error messages for improved clarity and consistency with documentation.
2+

0 commit comments

Comments
 (0)