From a458589a7e224cc45a868af2eb727e4f898e8d22 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Mon, 4 May 2026 15:22:54 -0700 Subject: [PATCH] =?UTF-8?q?[3.14]=20GH-130750:=20Restore=20quoting=20of=20?= =?UTF-8?q?choices=20in=20argparse=20error=20messag=E2=80=A6=20(#149385)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [3.14] GH-130750: Restore quoting of choices in argparse error messages to match documentation and improve clarity (GH-144983) (cherry picked from commit 53a7f76501923059188922be231db855265fe9a4) --- Lib/argparse.py | 2 +- Lib/test/test_argparse.py | 16 ++++++++-------- ...026-02-19-04-40-57.gh-issue-130750.0hW52O.rst | 2 ++ 3 files changed, 11 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-02-19-04-40-57.gh-issue-130750.0hW52O.rst diff --git a/Lib/argparse.py b/Lib/argparse.py index 8cf85694300..24f4740562d 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2678,7 +2678,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): diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 8331d021813..627e395c49b 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -1045,7 +1045,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'], ) @@ -2313,7 +2313,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 ) @@ -2323,7 +2323,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, ) @@ -2336,7 +2336,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, ) @@ -2348,7 +2348,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, ) @@ -2358,7 +2358,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, ) @@ -2378,7 +2378,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, ) @@ -2388,7 +2388,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, ) diff --git a/Misc/NEWS.d/next/Library/2026-02-19-04-40-57.gh-issue-130750.0hW52O.rst b/Misc/NEWS.d/next/Library/2026-02-19-04-40-57.gh-issue-130750.0hW52O.rst new file mode 100644 index 00000000000..8bca48ab159 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-19-04-40-57.gh-issue-130750.0hW52O.rst @@ -0,0 +1,2 @@ +Restore quoting of choices in :mod:`argparse` error messages for improved clarity and consistency with documentation. +