Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-117941: Fix argparse issue-Changed call method of BooleanOptionalAction class in argparse.py #117941 #118103

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jjpapa36912
Copy link

@jjpapa36912 jjpapa36912 commented Apr 19, 2024

Copy link

cpython-cla-bot bot commented Apr 19, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Apr 19, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@jjpapa36912
Copy link
Author

I was wondering if when does it review.

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

The PR needs tests and documentation for the new behaviour.

However, I'm not convinced that we should do this; I think it would be better to deprecate adding boolean options that start with --no-. The auto-added --no-no- option itself is surprising.

@@ -0,0 +1 @@
write!
Copy link
Member

Choose a reason for hiding this comment

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

Please see the devguide on how to write a NEWS entry.

@jjpapa36912
Copy link
Author

The PR needs tests and documentation for the new behaviour.

However, I'm not convinced that we should do this; I think it would be better to deprecate adding boolean options that start with --no-. The auto-added --no-no- option itself is surprising.

Thank you for answer my question. I understand your explanation. But I think that to eliminating exception or abnormal situation is very important. In that sense, isn't it better to fix the code?

@encukou
Copy link
Member

encukou commented Apr 23, 2024

If you can fix it properly, sure. It will not be easy.
As I said above, the PR needs tests and documentation for the new behaviour. When you write the tests, you'll probably discover many cases where this PR results in unexpected behaviour.

On the other hand, if we clearly say that adding --no- options directly doesn't work, there will be fewer fewer special cases that argparse needs to handle, test and document. Less code is better for everyone ;)

@jjpapa36912
Copy link
Author

If you can fix it properly, sure. It will not be easy. As I said above, the PR needs tests and documentation for the new behaviour. When you write the tests, you'll probably discover many cases where this PR results in unexpected behaviour.

On the other hand, if we clearly say that adding --no- options directly doesn't work, there will be fewer fewer special cases that argparse needs to handle, test and document. Less code is better for everyone ;)

Oh, thank you for your answer. I have one question. If the option string starts with "--no-" I think the current BoolOptionalAction always returns "False". What do you think about this?

@encukou
Copy link
Member

encukou commented Apr 24, 2024

Right; I don't think that was the intent.
But, I can see someone trying the following and finding that it works for them (with a weird auto-added --no-no-foo option, which you can ignore):

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--no-foo', default=True, action=argparse.BooleanOptionalAction)
>>> parser.parse_args()
Namespace(no_foo=True)
>>> parser.parse_args(['--no-foo'])
Namespace(no_foo=False)

The documentation doesn't clearly say that this is wrong:

You may also specify an arbitrary action by passing an Action subclass or other object that implements the same interface. The BooleanOptionalAction is available in argparse and adds support for boolean actions such as --foo and --no-foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants