Skip to content

Commit a02beab

Browse files
[3.13] GH-130750: Restore quoting of choices in argparse error messag… (#149386)
[3.13] 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 9f9a273 commit a02beab

3 files changed

Lines changed: 5 additions & 3 deletions

File tree

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2601,7 +2601,7 @@ def _check_value(self, action, value):
26012601
choices = iter(choices)
26022602
if value not in choices:
26032603
args = {'value': str(value),
2604-
'choices': ', '.join(map(str, action.choices))}
2604+
'choices': ', '.join(repr(str(choice)) for choice in action.choices)}
26052605
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
26062606
raise ArgumentError(action, msg % args)
26072607

Lib/test/test_argparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ def test_invalid_enum_value_raises_error(self):
10471047
parser.add_argument('--color', choices=self.Color)
10481048
self.assertRaisesRegex(
10491049
argparse.ArgumentError,
1050-
r"invalid choice: 'yellow' \(choose from red, green, blue\)",
1050+
r"invalid choice: 'yellow' \(choose from 'red', 'green', 'blue'\)",
10511051
parser.parse_args,
10521052
['--color', 'yellow'],
10531053
)
@@ -2517,7 +2517,7 @@ def test_wrong_argument_subparsers_no_destination_error(self):
25172517
parser.parse_args(('baz',))
25182518
self.assertRegex(
25192519
excinfo.exception.stderr,
2520-
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from foo, bar\)\n$"
2520+
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
25212521
)
25222522

25232523
def test_optional_subparsers(self):
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)