GH-130750: Restore quoting of choices in argparse error messages to match documentation and improve clarity#144983
GH-130750: Restore quoting of choices in argparse error messages to match documentation and improve clarity#144983savannahostrowski wants to merge 4 commits intopython:mainfrom
Conversation
sobolevn
left a comment
There was a problem hiding this comment.
I support explicit quotes, because bar in Python is a variable name, 'bar' is a string. 👍
|
@serhiy-storchaka Mind taking a look at this one, since you were involved in the merge of the PR that changed this display? |
|
Looking how is this handled in other programs. On Linux: On FreeBSD: Both quote the invalid value, but do not quote the list of valid values. |
|
There are also plenty of command-line tools that do quote valid choices in error messages, so it's a mixed bag across the ecosystem, I think. My preference would be to restore the previous quoting behavior here. The PR that changed the display wasn't really about removing quotes; it was about fixing StrEnum, and the loss of quotes was an acknowledged side effect. |
In 66b3922 (gh-117766), argparse was changed to use
str()instead ofrepr()when displaying choices in error messages. This was done to improve StrEnum display, but had the side effect of removing quotes from string choices, making error messages less clear and inconsistent with the documentation.This PR restores quoting and adds special handling for Enum members to display just the quoted value (e.g.,
'red'instead of<Color.RED: 'red'>). This also improves clarity for mixed-type choices (e.g.,1, '2'clearly shows int vs string).