[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 53a7f76501)
This commit is contained in:
Savannah Ostrowski
2026-05-04 16:18:05 -07:00
committed by GitHub
parent 9f9a273267
commit a02beabe2d
3 changed files with 5 additions and 3 deletions
+1 -1
View File
@@ -2601,7 +2601,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
choices = iter(choices)
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)')
raise ArgumentError(action, msg % args)
+2 -2
View File
@@ -1047,7 +1047,7 @@ class TestStrEnumChoices(TestCase):
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'],
)
@@ -2517,7 +2517,7 @@ class TestAddSubparsers(TestCase):
parser.parse_args(('baz',))
self.assertRegex(
excinfo.exception.stderr,
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from foo, bar\)\n$"
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
)
def test_optional_subparsers(self):
@@ -0,0 +1,2 @@
Restore quoting of choices in :mod:`argparse` error messages for improved clarity and consistency with documentation.