GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@mattwang44 mattwang44 commented Sep 5, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

(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.

  • Issue: argparse.BooleanOptionalAction silently failed when used with parser whose prefix_chars has no '-' #138525

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mattwang44
fix(argparse): raise error when BooleanOptionalAction used with prefi…
fb362be
…x_chars without '-'
@mattwang44
mattwang44 requested a review from savannahostrowski as a code owner September 5, 2025 06:57
@bedevere-app bedevere-app Bot added the awaiting review label Sep 5, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Sep 5, 2025
argparse.BooleanOptionalAction silently failed when used with parser whose prefix_chars has no '-' #138525
Closed
@mattwang44
fix linter warning
0d45340
@corona10

corona10 commented Sep 5, 2025

Copy link
Copy Markdown
Member

Would like to add NEWS.d to use blurb tool?

mattwang44 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@corona10 corona10 assigned savannahostrowski Sep 5, 2025
@mattwang44
chore: Add NEWS entry
2932858
@serhiy-storchaka

serhiy-storchaka commented Sep 5, 2025

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattwang44
add action method for parser compatibility check
c14013f
@mattwang44

mattwang44 commented Sep 7, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sobolevn
sobolevn reviewed Sep 8, 2025
View reviewed changes
Comment thread Lib/argparse.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2025-09-05-15-42-16.gh-issue-138525.ngZ0qT.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_argparse.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

mattwang44 and others added 4 commits September 8, 2025 21:45
@mattwang44 @sobolevn
fix(argparse): apply suggestions from code review
69d2c98
Co-authored-by: sobolevn <[email protected]>
@mattwang44 @sobolevn
fix(argparse): apply suggestions from code review
e8c1c94
Co-authored-by: sobolevn <[email protected]>
@mattwang44
fix(argparse): match message with assertRaisesRegex
9faaac7
@mattwang44
Merge branch 'main' into pythongh-138525
c6221f5
@savannahostrowski savannahostrowski mentioned this pull request Nov 22, 2025
gh-138525: Support single-dash long options and prefix_chars in BooleanOptionalAction #138692
Merged
@serhiy-storchaka

serhiy-storchaka commented Nov 22, 2025

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka serhiy-storchaka closed this Nov 22, 2025
@savannahostrowski

savannahostrowski commented Nov 22, 2025

Copy link
Copy Markdown
Member

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
All reactions
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattwang44
mattwang44 deleted the gh-138525 branch November 23, 2025 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sobolevn sobolevn sobolevn left review comments
@savannahostrowski savannahostrowski Awaiting requested review from savannahostrowski savannahostrowski is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@savannahostrowski savannahostrowski

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

argparse.BooleanOptionalAction silently failed when used with parser whose prefix_chars has no '-'

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@mattwang44 @corona10 @serhiy-storchaka @savannahostrowski @sobolevn
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.