-
-
Notifications
You must be signed in to change notification settings - Fork 805
π Make second column of Rich help output reflect the type consistently, even when using metavar
#1410
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
svlandeg
wants to merge
13
commits into
fastapi:master
Choose a base branch
from
svlandeg:fix/metavar
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.
+172
β45
Open
π Make second column of Rich help output reflect the type consistently, even when using metavar
#1410
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
adc39e2
add two more unit tests for rich formatting
svlandeg fa7d8d5
add test from #438
svlandeg 6bb0977
test correct usage string
svlandeg 13bd5e4
fix usage string
svlandeg a6282f7
expand new unit test
svlandeg 5b105f6
fix newline
svlandeg 8f8f080
change metavar column into type column (WIP)
svlandeg 98b8469
fix other tests that didn't check for the right capitalization
svlandeg a6314ca
depending on argument/option, parse the metavar string differently
svlandeg b7d902b
fix one more old test
svlandeg da5e707
fix comments related to PR 1409
svlandeg 0208421
pragma no cover for example app output
svlandeg 987eaaf
add no cover to L390
svlandeg 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
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
37 changes: 37 additions & 0 deletions
37
tests/test_tutorial/test_arguments/test_help/test_tutorial006_an_rich.py
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,37 @@ | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import typer | ||
| from typer.testing import CliRunner | ||
|
|
||
| from docs_src.arguments.help import tutorial006_an as mod | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
| app = typer.Typer(rich_markup_mode="rich") | ||
| app.command()(mod.main) | ||
|
|
||
|
|
||
| def test_help(): | ||
| result = runner.invoke(app, ["--help"]) | ||
| assert result.exit_code == 0 | ||
| assert "[OPTIONS] β¨userβ¨" in result.output | ||
| assert "Arguments" in result.output | ||
| assert "β¨userβ¨" in result.output | ||
| assert "name" not in result.output | ||
| assert "[default: World]" in result.output | ||
|
|
||
|
|
||
| def test_call_arg(): | ||
| result = runner.invoke(app, ["Camila"]) | ||
| assert result.exit_code == 0 | ||
| assert "Hello Camila" in result.output | ||
|
|
||
|
|
||
| def test_script(): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout |
37 changes: 37 additions & 0 deletions
37
tests/test_tutorial/test_arguments/test_help/test_tutorial006_rich.py
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,37 @@ | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import typer | ||
| from typer.testing import CliRunner | ||
|
|
||
| from docs_src.arguments.help import tutorial006 as mod | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
| app = typer.Typer(rich_markup_mode="rich") | ||
| app.command()(mod.main) | ||
|
|
||
|
|
||
| def test_help(): | ||
| result = runner.invoke(app, ["--help"]) | ||
| assert result.exit_code == 0 | ||
| assert "[OPTIONS] β¨userβ¨" in result.output | ||
| assert "Arguments" in result.output | ||
| assert "β¨userβ¨" in result.output | ||
| assert "name" not in result.output | ||
| assert "[default: World]" in result.output | ||
|
|
||
|
|
||
| def test_call_arg(): | ||
| result = runner.invoke(app, ["Camila"]) | ||
| assert result.exit_code == 0 | ||
| assert "Hello Camila" in result.output | ||
|
|
||
|
|
||
| def test_script(): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout |
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 |
|---|---|---|
|
|
@@ -35,8 +35,8 @@ | |
| STYLE_SWITCH = "bold green" | ||
| STYLE_NEGATIVE_OPTION = "bold magenta" | ||
| STYLE_NEGATIVE_SWITCH = "bold red" | ||
| STYLE_METAVAR = "bold yellow" | ||
| STYLE_METAVAR_SEPARATOR = "dim" | ||
| STYLE_TYPES = "bold yellow" | ||
| STYLE_TYPES_SEPARATOR = "dim" | ||
| STYLE_USAGE = "yellow" | ||
| STYLE_USAGE_COMMAND = "bold" | ||
| STYLE_DEPRECATED = "red" | ||
|
|
@@ -113,7 +113,7 @@ class OptionHighlighter(RegexHighlighter): | |
| highlights = [ | ||
| r"(^|\W)(?P<switch>\-\w+)(?![a-zA-Z0-9])", | ||
| r"(^|\W)(?P<option>\-\-[\w\-]+)(?![a-zA-Z0-9])", | ||
| r"(?P<metavar>\<[^\>]+\>)", | ||
| r"(?P<types>\<[^\>]+\>)", | ||
| r"(?P<usage>Usage: )", | ||
| ] | ||
|
|
||
|
|
@@ -137,8 +137,8 @@ def _get_rich_console(stderr: bool = False) -> Console: | |
| "switch": STYLE_SWITCH, | ||
| "negative_option": STYLE_NEGATIVE_OPTION, | ||
| "negative_switch": STYLE_NEGATIVE_SWITCH, | ||
| "metavar": STYLE_METAVAR, | ||
| "metavar_sep": STYLE_METAVAR_SEPARATOR, | ||
| "types": STYLE_TYPES, | ||
| "types_sep": STYLE_TYPES_SEPARATOR, | ||
| "usage": STYLE_USAGE, | ||
| }, | ||
| ), | ||
|
|
@@ -361,38 +361,47 @@ def _print_options_panel( | |
| opt_short_strs = [] | ||
| secondary_opt_long_strs = [] | ||
| secondary_opt_short_strs = [] | ||
|
|
||
| # check whether argument has a metavar name or type set | ||
| metavar_name = None | ||
| metavar_type = None | ||
| # TODO: when deprecating Click < 8.2, make ctx required | ||
| signature = inspect.signature(param.make_metavar) | ||
| if "ctx" in signature.parameters: | ||
| metavar_str = param.make_metavar(ctx=ctx) | ||
| else: | ||
| # Click < 8.2 | ||
| metavar_str = param.make_metavar() # type: ignore[call-arg] | ||
| if isinstance(param, click.Argument): | ||
| metavar_name = metavar_str | ||
| if isinstance(param, click.Option): | ||
| metavar_type = metavar_str | ||
|
Comment on lines
+377
to
+378
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this part is crucial to ensure the current |
||
|
|
||
| for opt_str in param.opts: | ||
| if "--" in opt_str: | ||
| opt_long_strs.append(opt_str) | ||
| elif metavar_name: | ||
| opt_short_strs.append(metavar_name) | ||
| else: | ||
| opt_short_strs.append(opt_str) | ||
| for opt_str in param.secondary_opts: | ||
| if "--" in opt_str: | ||
| secondary_opt_long_strs.append(opt_str) | ||
| elif metavar_name: | ||
| secondary_opt_short_strs.append(metavar_name) | ||
| else: | ||
| secondary_opt_short_strs.append(opt_str) | ||
|
|
||
| # Column for a metavar, if we have one | ||
| metavar = Text(style=STYLE_METAVAR, overflow="fold") | ||
| # TODO: when deprecating Click < 8.2, make ctx required | ||
| signature = inspect.signature(param.make_metavar) | ||
| if "ctx" in signature.parameters: | ||
| metavar_str = param.make_metavar(ctx=ctx) | ||
| else: | ||
| # Click < 8.2 | ||
| metavar_str = param.make_metavar() # type: ignore[call-arg] | ||
| # Column for recording the type | ||
| types = Text(style=STYLE_TYPES, overflow="fold") | ||
|
|
||
| # Do it ourselves if this is a positional argument | ||
| if ( | ||
| isinstance(param, click.Argument) | ||
| and param.name | ||
| and metavar_str == param.name.upper() | ||
| ): | ||
| metavar_str = param.type.name.upper() | ||
|
|
||
| # Skip booleans and choices (handled above) | ||
| if metavar_str != "BOOLEAN": | ||
| metavar.append(metavar_str) | ||
| # Fetch type | ||
| if metavar_type and metavar_type != "BOOLEAN": | ||
| types.append(metavar_type) | ||
| else: | ||
| type_str = param.type.name.upper() | ||
| if type_str != "BOOLEAN": | ||
| types.append(type_str) | ||
|
|
||
| # Range - from | ||
| # https://github.com/pallets/click/blob/c63c70dabd3f86ca68678b4f00951f78f52d0270/src/click/core.py#L2698-L2706 # noqa: E501 | ||
|
|
@@ -404,22 +413,22 @@ def _print_options_panel( | |
| ): | ||
| range_str = param.type._describe_range() | ||
| if range_str: | ||
| metavar.append(RANGE_STRING.format(range_str)) | ||
| types.append(RANGE_STRING.format(range_str)) | ||
|
|
||
| # Required asterisk | ||
| required: Union[str, Text] = "" | ||
| if param.required: | ||
| required = Text(REQUIRED_SHORT_STRING, style=STYLE_REQUIRED_SHORT) | ||
|
|
||
| # Highlighter to make [ | ] and <> dim | ||
| class MetavarHighlighter(RegexHighlighter): | ||
| class TypesHighlighter(RegexHighlighter): | ||
| highlights = [ | ||
| r"^(?P<metavar_sep>(\[|<))", | ||
| r"(?P<metavar_sep>\|)", | ||
| r"(?P<metavar_sep>(\]|>)$)", | ||
| r"^(?P<types_sep>(\[|<))", | ||
| r"(?P<types_sep>\|)", | ||
| r"(?P<types_sep>(\]|>)$)", | ||
| ] | ||
|
|
||
| metavar_highlighter = MetavarHighlighter() | ||
| types_highlighter = TypesHighlighter() | ||
|
|
||
| required_rows.append(required) | ||
| options_rows.append( | ||
|
|
@@ -428,7 +437,7 @@ class MetavarHighlighter(RegexHighlighter): | |
| highlighter(",".join(opt_short_strs)), | ||
| negative_highlighter(",".join(secondary_opt_long_strs)), | ||
| negative_highlighter(",".join(secondary_opt_short_strs)), | ||
| metavar_highlighter(metavar), | ||
| types_highlighter(types), | ||
| _get_parameter_help( | ||
| param=param, | ||
| ctx=ctx, | ||
|
|
||
Oops, something went wrong.
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.
The commented line on L123 and L133 would be the correct
assertonce #1409 is merged. Suggest to review #1409 first before this one.