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-88753: Make BooleanOptionalAction's addition of default to help more similar to other actions #27808

Merged
merged 2 commits into from
May 3, 2022

Conversation

abadger
Copy link
Contributor

@abadger abadger commented Aug 18, 2021

Help for other actions omit the default value if default is SUPPRESS or
already contains the special format string '%(default)'. Add those
special cases to BooleanOptionalAction's help formatting too.

Other actions emit (default: None) when the default value is None. This
allows documenting that the unset case is treated differently. Add this
functionality to BooleanOptionalAction as well.

Fixes https://bugs.python.org/issue44587 so that default=SUPPRESS is not
emitted.

Fixes https://bugs.python.org/issue38956 as this code will detect
whether '%(default)s' has already been specified in the help string.

https://bugs.python.org/issue44587

Fixes: #88753

@@ -3603,9 +3603,10 @@ class TestHelpUsage(HelpTestCase):
-h, --help show this help message and exit
-w W [W ...] w
-x [X ...] x
--foo, --no-foo Whether to foo
--foo, --no-foo Whether to foo (default: None)
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO showing None here is just as confusing as SUPRESS. Because, I mean, from the tool user's perspective, what does it mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just like any other argument, I believe conveying the meaning to the user is the responsibility of the description:

--uid                     The user id to assign to the user.  If None, then let the
                          system assign the next available value. (default: None)
--hide, --no-hide-user    Whether to omit the user from the graphical system login.
                          If None, then use the system default (default: None)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

However, I think this is a good example of why I mentioned that BooleanOptionalArgument adding defaults should be revisited. The argparse architecture gives the programmer the ability to use a formatter_class. If you want to display default values for your arguments but not if they were None, then you can write your own HelpFormatter to do that. Since BooleanOptionalArguments modifies the help string on its own, not using a formatter class, the programmer cannot use argparse's standard features to choose different cases where the default should not be emitted.

Removing this formatting feature from BooleanOptionalArguments gives the programmer the flexibility to choose a formatter that does what they decide is appropriate. Leaving it in means that you'll always be stuck with someone having to work around the choices that BooleanOptionalArguments makes.

Copy link
Contributor

Choose a reason for hiding this comment

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

abadger: it may be better then to avoid displaying default value because the author of the help string can integrate it into the help, if he feels it's needed. In other words, when help message is not provided, it will be less confusing; if the author feels it's important enough to explain the effect of special value, they will test the output and see that default value needs to be integrated into the help. In either case, confusion will be avoided.

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Sep 19, 2021
@MaxwellDupre
Copy link
Contributor

Suggest a change to docs is worth a mention.

@abadger abadger marked this pull request as draft May 2, 2022 16:42
@abadger
Copy link
Contributor Author

abadger commented May 2, 2022

Working on first rebasing this and then changing according to @ambv's suggestion

@abadger abadger force-pushed the fix-booleanoptionalaction-help branch 3 times, most recently from d5fd5a9 to 9d00eca Compare May 2, 2022 19:44
@abadger abadger changed the title bpo-44587: Make BooleanOptionalAction's addition of default to help more similar to other actions gh-88753: Make BooleanOptionalAction's addition of default to help more similar to other actions May 2, 2022
@abadger abadger force-pushed the fix-booleanoptionalaction-help branch from 9d00eca to 7dd047c Compare May 2, 2022 22:16
@abadger abadger marked this pull request as ready for review May 2, 2022 22:42
abadger and others added 2 commits May 2, 2022 15:42
… to other actions.

Help for other actions omit the default value if default is SUPPRESS or
already contains the special format string '%(default)'.  Add those
special cases to BooleanOptionalAction's help formatting too.

Other actions emit (default: None) when the default value is None.  This
allows documenting that the unset case is treated differently.  Add this
functionality to BooleanOptionalAction as well.

Fixes https://bugs.python.org/issue44587 so that default=SUPPRESS is not
emitted.

Fixes https://bugs.python.org/issue38956 as this code will detect
whether '%(default)s' has already been specified in the help string.
@abadger abadger force-pushed the fix-booleanoptionalaction-help branch from 792082f to dc4babf Compare May 2, 2022 22:42
@abadger
Copy link
Contributor Author

abadger commented May 2, 2022

Okay, the original strategy which includes the default value in the help message unconditionally is in the first commit. The second commit changes this to do as @akulakov proposed and takes the default value out of the help message. People can use ArgumentDefaultsHelpFormatter if they want that behaviour.

This is based on @michiboo 's earlier PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Stale PR or inactive for long period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

argparse BooleanOptionalAction displays default=SUPPRESS unlike other action types
7 participants