Skip to content

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

@ambv
Copy link
Contributor

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
@miss-islington
Copy link
Contributor

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

@miss-islington
Copy link
Contributor

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

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

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Aug 16, 2021
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 16, 2021
…H-27672)

Co-authored-by: Micky Yun Chan <[email protected]>
(cherry picked from commit 1512bc2)

Co-authored-by: Maximilian Hils <[email protected]>
ambv pushed a commit to ambv/cpython that referenced this pull request Aug 16, 2021
…ythonGH-27672)

Co-authored-by: Micky Yun Chan <[email protected]>.
(cherry picked from commit 1512bc2)

Co-authored-by: Maximilian Hils <[email protected]>
@bedevere-bot
Copy link

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

@abadger
Copy link
Contributor

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-build@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 August 16, 2021 22:32
@mhils
Copy link
Contributor Author

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 commented Aug 17, 2021 via email

@ambv
Copy link
Contributor

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 pull request Aug 17, 2021
…H-27672) (GH-27788)

Co-authored-by: Micky Yun Chan <[email protected]>.
(cherry picked from commit 1512bc2)

Co-authored-by: Maximilian Hils <[email protected]>
miss-islington added a commit that referenced this pull request Aug 17, 2021
Co-authored-by: Micky Yun Chan <[email protected]>
(cherry picked from commit 1512bc2)

Co-authored-by: Maximilian Hils <[email protected]>
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.

8 participants