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

bpo-38956: don't print BooleanOptionalAction's default twice #27672

Merged
merged 4 commits into from Aug 16, 2021

Conversation

@mhils
Copy link
Contributor

@mhils mhils commented Aug 9, 2021

This PR is a less intrusive fix to not double-print the default value when using argparse.BooleanOptionalAction with ArgumentDefaultsHelpFormatter. The original bug report (bpo-38956) was about removing the default entirely, but @rhettinger made a sensible point in #17447 (comment) that a default should always been shown here. While #17447 seems to work as intended, progress stalled a bit presumably because it's not entirely trivial to review. This PR proposes a more straightforward attempt to solve the problem, which can hopefully be reviewed more easily. If the maintainers ultimately prefers #17447 that is perfectly fine with me of course. 😃

In short, in this PR we make use of the fact that ArgumentDefaultsHelpFormatter already checks if the default is already included:

if '%(default)' not in action.help:

This condition previously did not trigger because BooleanOptionalAction already did the string interpolation directly.

https://bugs.python.org/issue38956

@mhils mhils force-pushed the show-default branch from 884f08f to 94a32b8 Aug 9, 2021
@ambv
Copy link
Contributor

@ambv ambv commented Aug 16, 2021

Confirmed that the test fails without the source change:

-   --taz, --no-taz  Whether to taz it (default: True)
+   --taz, --no-taz  Whether to taz it (default: True) (default: True)
?                                                ++++++++++++++++
    --quux QUUX      Set the quux (default: 42)

(that diffing algorithm is weird)

@ambv ambv merged commit 1512bc2 into python:main Aug 16, 2021
13 checks passed
@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Aug 16, 2021

Thanks @mhils for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10.
🐍🍒⛄1�7🤖

@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Aug 16, 2021

Sorry, @mhils and @ambv, I could not cleanly backport this to 3.9 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 1512bc21d60f098a9e9f37b44a2f6a9b49a3fd4f 3.9

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Aug 16, 2021

GH-27787 is a backport of this pull request to the 3.10 branch.

miss-islington added a commit to miss-islington/cpython that referenced this issue Aug 16, 2021
…H-27672)

Co-authored-by: Micky Yun Chan <michan@redhat.com>
(cherry picked from commit 1512bc2)

Co-authored-by: Maximilian Hils <git@maximilianhils.com>
ambv added a commit to ambv/cpython that referenced this issue Aug 16, 2021
…ythonGH-27672)

Co-authored-by: Micky Yun Chan <michan@redhat.com>.
(cherry picked from commit 1512bc2)

Co-authored-by: Maximilian Hils <git@maximilianhils.com>
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Aug 16, 2021

GH-27788 is a backport of this pull request to the 3.9 branch.

@abadger
Copy link
Contributor

@abadger abadger commented Aug 16, 2021

Hi, I don't think this was the right fix for this issue (or maybe better stated as... this is an incomplete fix that leaves two diverging code paths which should be consolidated). Please see https://bugs.python.org/issue44587 for my explanation of what I think should be done (Option 2 if we want to go the nonintrusive route). I have an implementation of option 2 vendored into one of my projects here: ansible-community/antsibull@67f6d1a#diff-f515d1b9734c55a77e0a94b093b41eb1102a986be60631df39ac1513895aba07 I can pull that out into a pr but I had been waiting on someone to confirm that Option 2 is really the desired outcome considering the architecture of argparse.

@mhils mhils deleted the show-default branch Aug 16, 2021
@mhils
Copy link
Contributor Author

@mhils mhils commented Aug 16, 2021

Thanks @ambv! ❤️

@abadger: My intention with this PR was to unblock a somewhat stalled issue with changes that are trivial to review. bpo-44587 wasn't linked at bpo-38956 so I wasn't aware of it. The changes here shouldn't affect the usefulness of what you proposed, I'd suggest you go ahead and open a PR for your option 2. :)

@abadger
Copy link
Contributor

@abadger abadger commented Aug 17, 2021

@ambv
Copy link
Contributor

@ambv ambv commented Aug 17, 2021

@abadger, this was merged because it's a rather trivial improvement over the status quo. I intend to also release this fix in 3.9.7 on August 30th. Your idea in BPO-44587 to further improve this while retaining displaying the default value is interesting. If you prepare a PR we can work with, we'll look into merging that as well.

ambv added a commit that referenced this issue Aug 17, 2021
…H-27672) (GH-27788)

Co-authored-by: Micky Yun Chan <michan@redhat.com>.
(cherry picked from commit 1512bc2)

Co-authored-by: Maximilian Hils <git@maximilianhils.com>
miss-islington added a commit that referenced this issue Aug 17, 2021
Co-authored-by: Micky Yun Chan <michan@redhat.com>
(cherry picked from commit 1512bc2)

Co-authored-by: Maximilian Hils <git@maximilianhils.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants