GH-130750: Restore quoting of choices in argparse error messages to match documentation and improve clarity (#144983)

This commit is contained in:
Savannah Ostrowski
2026-05-04 14:51:48 -07:00
committed by GitHub
parent 7acee984e8
commit 53a7f76501
4 changed files with 12 additions and 10 deletions
+1 -1
View File
@@ -2758,7 +2758,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
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):
+8 -8
View File
@@ -1123,7 +1123,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'],
)
@@ -2392,7 +2392,7 @@ class TestArgumentAndSubparserSuggestions(TestCase):
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
)
@@ -2402,7 +2402,7 @@ class TestArgumentAndSubparserSuggestions(TestCase):
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,
)
@@ -2415,7 +2415,7 @@ class TestArgumentAndSubparserSuggestions(TestCase):
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,
)
@@ -2427,7 +2427,7 @@ class TestArgumentAndSubparserSuggestions(TestCase):
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,
)
@@ -2438,7 +2438,7 @@ class TestArgumentAndSubparserSuggestions(TestCase):
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,
)
@@ -2458,7 +2458,7 @@ class TestArgumentAndSubparserSuggestions(TestCase):
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,
)
@@ -2468,7 +2468,7 @@ class TestArgumentAndSubparserSuggestions(TestCase):
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,
)
+1 -1
View File
@@ -359,7 +359,7 @@ class TestTimeit(unittest.TestCase):
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):
@@ -0,0 +1,2 @@
Restore quoting of choices in :mod:`argparse` error messages for improved clarity and consistency with documentation.