-
Notifications
You must be signed in to change notification settings - Fork 8
move formatting of defaults to FormatterClass #65
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
Open
MikkelSchubert
wants to merge
6
commits into
typed-argparse:master
Choose a base branch
from
MikkelSchubert:default_formatter_class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10c8c7b
move formatting of defaults to FormatterClass
MikkelSchubert 8a76abd
black
MikkelSchubert 5aad686
Merge branch 'typed-argparse:master' into default_formatter_class
MikkelSchubert 7c9484d
exclude True/False values when printing defaults
MikkelSchubert ed76da7
fix auto_default_help with argparse.SUPPRESS
MikkelSchubert 46a585a
explain the need for checking for SUPPRESS
MikkelSchubert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import argparse | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class DefaultHelpFormatter(argparse.HelpFormatter): | ||
| class Unformatted(str): | ||
| pass | ||
|
|
||
| def _get_help_string(self, action: argparse.Action) -> Optional[str]: | ||
| if ( | ||
| not isinstance(action.help, DefaultHelpFormatter.Unformatted) | ||
| and action.help is not None | ||
| and action.default is not None | ||
| and action.default is not True | ||
| and action.default is not False | ||
| # Setting default=SUPPRESS is not supported by typed-argparse, but is used | ||
| # by argparse for the help (-h/--help) and version (-v/--version) actions. | ||
| and action.default is not argparse.SUPPRESS | ||
| ): | ||
| try: | ||
| from yachalk import chalk # pyright: ignore | ||
|
|
||
| return f"{action.help} {chalk.gray(f'[default: {action.default}]')}" | ||
| except ImportError: | ||
| return f"{action.help} [default: {action.default}]" | ||
| else: | ||
| return action.help |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few things that I don't understand here yet:
It looks like we are going into the else branch if
helpisDefaultHelpFormatter.Unformattedor if it isNone, right? Doesn't that mean thatDefaultHelpFormatter.Unformattedis unnecessary? I.e., could the user achieve the same by settinghelptoNoneanyway?What is the purpose of comparing
action.defaulttoargparse.SUPPRESS? In general it seems thatargparse.SUPPRESShas multiple roles in argparse:helpargument to suppress the help text.argument_defaultanddefaultarguments (for the parser or add_argument). This usage has a special meaning that the field will be omitted from theargparse.Namespaceinstance.The latter usage is problematic from the typed_argparse perspective, because we rely on arguments always being generated in the
Namespaceinstance. Usingargparse.SUPPRESSin the default position actually breaks argument parsing currently:Fortunately it doesn't type check for other types than
str, because its a string based sentinel value. I'll probably have to catch that, i.e., the default value'==SUPPRESS=='for strings is kinda forbidden 😕This makes we wonder why to compare with
action.defaulthere though. I would have understood if there is a comparison likeaction.help == argparse.SUPPRESSbut surprisingly that doesn't seem necessary - so the filtering actually happens internally in argparse? If so, shouldn't we removeaction.default is not argparse.SUPPRESSfrom the condition, if we cannot support it in thedefaultposition anyway?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't worry finding the time for this. There's no hurry.
DefaultHelpFormatter.Unformattedis a bit of a hack to support theauto_default_helpoption and is only used ifhelpis not None (see https://github.com/MikkelSchubert/typed-argparse/blob/8a76abd4166a1bcbdd16554ce56851516bcb42be/typed_argparse/parser.py#L480). The problem is thatauto_default_helpisn't available to theHelpFormatterinstance, so instead I convert the (not None) help string to an instance of a string subclass. This allows me to check whether or not the user setauto_default_help=Truewithout breaking stuff in argparse, wherehelpis expected to be a string. There may be a better way of doing this, but I couldn't figure one out.I agree with all your points regarding
argparse.SUPPRESS, but it needs to be handled here simply because argparse makes use of it even if we don't. I should probably add a comment about that. If you don't check for it in theHelpFormatterthen you get output like this from (from an unmodifiedexamples/high_level_api/basic_usage.py):