https://github.com/python/cpython/commit/a02beabe2d6a9601b69054eab268c2606c5837f4
commit: a02beabe2d6a9601b69054eab268c2606c5837f4
branch: 3.13
author: Savannah Ostrowski <[email protected]>
committer: savannahostrowski <[email protected]>
date: 2026-05-04T23:18:05Z
summary:
[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 53a7f76501923059188922be231db855265fe9a4)
files:
A Misc/NEWS.d/next/Library/2026-02-19-04-40-57.gh-issue-130750.0hW52O.rst
M Lib/argparse.py
M Lib/test/test_argparse.py
diff --git a/Lib/argparse.py b/Lib/argparse.py
index a0faa3f41e26b0..7d845175644b8f 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -2601,7 +2601,7 @@ def _check_value(self, action, value):
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)
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 91475180a1d652..d118f334c0b089 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -1047,7 +1047,7 @@ def test_invalid_enum_value_raises_error(self):
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 @@ def
test_wrong_argument_subparsers_no_destination_error(self):
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
index 00000000000000..8bca48ab159476
--- /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.
+
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]