Skip to content

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

Merged
savannahostrowski merged 11 commits intopython:mainfrom
savannahostrowski:fix-suggestion
May 4, 2026
Merged

GH-130750: Restore quoting of choices in argparse error messages to match documentation and improve clarity#144983
savannahostrowski merged 11 commits intopython:mainfrom
savannahostrowski:fix-suggestion

Conversation

@savannahostrowski
Copy link
Copy Markdown
Member

@savannahostrowski savannahostrowski commented Feb 19, 2026

In 66b3922 (gh-117766), argparse was changed to use str() instead of repr() 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.

@savannahostrowski savannahostrowski added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Feb 19, 2026
@savannahostrowski savannahostrowski changed the title GH-130751: Restore quoting of choices in argparse error messages to match documentation and improve clarity GH-130750: Restore quoting of choices in argparse error messages to match documentation and improve clarity Feb 19, 2026
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I support explicit quotes, because bar in Python is a variable name, 'bar' is a string. 👍

@savannahostrowski
Copy link
Copy Markdown
Member Author

@serhiy-storchaka Mind taking a look at this one, since you were involved in the merge of the PR that changed this display?

@serhiy-storchaka
Copy link
Copy Markdown
Member

Looking how is this handled in other programs. On Linux:

$ ls --color=xyz
error: invalid value 'xyz' for '--color[=<color>]'

  [possible values: always, auto, never]

For more information, try '--help'.

On FreeBSD:

$ ls --color=xyz
ls: unsupported --color value 'xyz' (must be always, auto, or never)

Both quote the invalid value, but do not quote the list of valid values.

@savannahostrowski
Copy link
Copy Markdown
Member Author

savannahostrowski commented Feb 23, 2026

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.

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, argparce deviated enough from the original getopt.c, so we can not follow it in this particular detail.

Comment thread Lib/argparse.py Outdated
Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@savannahostrowski savannahostrowski enabled auto-merge (squash) May 4, 2026 20:55
@savannahostrowski savannahostrowski merged commit 53a7f76 into python:main May 4, 2026
52 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @savannahostrowski for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@miss-islington-app
Copy link
Copy Markdown

Sorry, @savannahostrowski, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 53a7f76501923059188922be231db855265fe9a4 3.14

@miss-islington-app
Copy link
Copy Markdown

Sorry, @savannahostrowski, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 53a7f76501923059188922be231db855265fe9a4 3.13

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 4, 2026

GH-149385 is a backport of this pull request to the 3.14 branch.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 4, 2026

GH-149386 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label May 4, 2026
savannahostrowski added a commit that referenced this pull request May 4, 2026
#149385)

[3.14] GH-130750: Restore quoting of choices in argparse error messages to match documentation and improve clarity (GH-144983)
(cherry picked from commit 53a7f76)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants