]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] GH-130750: Restore quoting of choices in argparse error messag… (#149386)
authorSavannah Ostrowski <savannah@python.org>
Mon, 4 May 2026 23:18:05 +0000 (16:18 -0700)
committerGitHub <noreply@github.com>
Mon, 4 May 2026 23:18:05 +0000 (23:18 +0000)
[3.13] 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
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2026-02-19-04-40-57.gh-issue-130750.0hW52O.rst [new file with mode: 0644]

index a0faa3f41e26b0f66922faec634b52183f10a77c..7d845175644b8f532624b17e50d8a88069a1f673 100644 (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)
 
index 91475180a1d6523e5abd77d096f6e9a942c7ea4c..d118f334c0b089d7b41558cfefbd674cd7504eff 100644 (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):
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 (file)
index 0000000..8bca48a
--- /dev/null
@@ -0,0 +1,2 @@
+Restore quoting of choices in :mod:`argparse` error messages for improved clarity and consistency with documentation.
+