GeezerVison is showing github.com — gh-138525: raise error when BooleanOptionalAction used with prefix_chars without '-' by mattwang44 · Pull Request #138526 · python/cpython
Conversation
mattwang44
commented
Sep 5, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
(this contribution is made during PyCon TW sprint)
fixes #138525. Here's the output of my local build with this patch.
>>> from argparse import BooleanOptionalAction, ArgumentParser
>>> p = ArgumentParser(prefix_chars='+')
>>> p.add_argument("++foo", action=BooleanOptionalAction)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
p.add_argument("++foo", action=BooleanOptionalAction)
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/whw/Desktop/cpython/Lib/argparse.py", line 1554, in add_argument
action._check_compatibility(self)
~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "/Users/whw/Desktop/cpython/Lib/argparse.py", line 969, in _check_compatibility
raise ValueError(f"BooleanOptionalAction requires '-' in prefix_chars.")
ValueError: BooleanOptionalAction requires '-' in prefix_chars.
I believe the description of BooleanOptionalAction on the argparse doc has already implied that it should be used with prefix_chars containing '-', so I didn't make any change to the docs.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Would like to add NEWS.d to use blurb tool?
|
mattwang44 reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Isn't it too late? It raises an error when the option is used, not when it is defined.
|
mattwang44 reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
mattwang44
commented
Sep 7, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Isn't it too late? It raises an error when the option is used, not when it is defined.
I find it difficult to achieve this without changing the interface. I've added a generic _check_compatibility() validation hook in the Action base class that gets called after action creation in add_argument(), allowing BooleanOptionalAction to validate itself against the parser's prefix_chars without leaking action-specific knowledge into _ActionsContainer. PTAL.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
Thank you for your PR, @mattwang44, but after adding support of alternate prefix chars in #138692 this check is no longer needed.
|
mattwang44 reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Hey @mattwang44! Thanks for opening the original issue and opening a PR!
If you're interested in continuing to contribute to argparse, we have a project board tracking open argparse issues that could use attention. There are issues ranging from small fixes/good first issues to larger enhancements, and I'm happy to help guide you through any of them if you'd like to pick something up.
Thanks again for your contribution, and I hope to see more PRs from you in the future!
|
corona10 and mattwang44 reacted with heart emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
savannahostrowski
Awaiting requested review from savannahostrowski
savannahostrowski is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|