-
-
Notifications
You must be signed in to change notification settings - Fork 716
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
✨ Upgrade autocompletion
functionality for compatibility with shell_complete
#1006
Open
bckohan
wants to merge
13
commits into
fastapi:master
Choose a base branch
from
bckohan:feat/autocompletion
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 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
860c392
allow Parameter to be acception by autocompletion functions, also pas…
bckohan 40fa0d5
add reusable completer function tutorial, add tutorial test for coverage
bckohan c2ffc6b
update completion_no_types tests to check for param as well
bckohan 46c406b
remove python 3.7 tabs for reusable completer example
bckohan 031a59e
Merge branch 'master' into feat/autocompletion
bckohan 05cb6f0
Merge branch 'master' into feat/autocompletion
bckohan 4c356f5
Merge branch 'master' into feat/autocompletion
bckohan c4ef5ab
revert bug fix for args parameter in completer functions, this will b…
bckohan 3c4bff4
fix linting error introduced on last commit
bckohan a60d98c
Merge branch 'master' into feat/autocompletion
bckohan bd49bcf
Merge branch 'master' into feat/autocompletion
bckohan 793921e
Merge branch 'master' into feat/autocompletion
bckohan 723cf94
Merge branch 'master' into feat/autocompletion
bckohan 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 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 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 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 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,38 @@ | ||
from typing import List | ||
|
||
import click | ||
import typer | ||
from click.shell_completion import CompletionItem | ||
|
||
valid_completion_items = [ | ||
("Camila", "The reader of books."), | ||
("Carlos", "The writer of scripts."), | ||
("Sebastian", "The type hints guy."), | ||
] | ||
|
||
|
||
def complete_name(ctx: typer.Context, param: click.Parameter, incomplete: str): | ||
names = (ctx.params.get(param.name) if param.name else []) or [] | ||
for name, help_text in valid_completion_items: | ||
if name.startswith(incomplete) and name not in names: | ||
yield CompletionItem(name, help=help_text) | ||
|
||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def main( | ||
name: List[str] = typer.Option( | ||
["World"], help="The name to say hi to.", autocompletion=complete_name | ||
), | ||
greeter: List[str] = typer.Option( | ||
None, help="Who are the greeters?.", autocompletion=complete_name | ||
), | ||
): | ||
for n in name: | ||
print(f"Hello {n}, from {' and '.join(greeter or [])}") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
This file contains 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,41 @@ | ||
from typing import List | ||
|
||
import click | ||
import typer | ||
from click.shell_completion import CompletionItem | ||
from typing_extensions import Annotated | ||
|
||
valid_completion_items = [ | ||
("Camila", "The reader of books."), | ||
("Carlos", "The writer of scripts."), | ||
("Sebastian", "The type hints guy."), | ||
] | ||
|
||
|
||
def complete_name(ctx: typer.Context, param: click.Parameter, incomplete: str): | ||
names = (ctx.params.get(param.name) if param.name else []) or [] | ||
for name, help_text in valid_completion_items: | ||
if name.startswith(incomplete) and name not in names: | ||
yield CompletionItem(name, help=help_text) | ||
|
||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def main( | ||
name: Annotated[ | ||
List[str], | ||
typer.Option(help="The name to say hi to.", autocompletion=complete_name), | ||
] = ["World"], | ||
greeter: Annotated[ | ||
List[str], | ||
typer.Option(help="Who are the greeters?.", autocompletion=complete_name), | ||
] = [], | ||
): | ||
for n in name: | ||
print(f"Hello {n}, from {' and '.join(greeter)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
This file contains 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 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 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 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 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 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 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 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
90 changes: 90 additions & 0 deletions
90
tests/test_tutorial/test_options_autocompletion/test_tutorial010.py
This file contains 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,90 @@ | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
from typer.testing import CliRunner | ||
|
||
from docs_src.options_autocompletion import tutorial010 as mod | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_completion(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, " "], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_TUTORIAL010.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "tutorial010.py --name Sebastian --name ", | ||
}, | ||
) | ||
assert '"Camila":"The reader of books."' in result.stdout | ||
assert '"Carlos":"The writer of scripts."' in result.stdout | ||
assert '"Sebastian":"The type hints guy."' not in result.stdout | ||
|
||
|
||
def test_completion_greeter1(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, " "], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_TUTORIAL010.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "tutorial010.py --name Sebastian --greeter Ca", | ||
}, | ||
) | ||
assert '"Camila":"The reader of books."' in result.stdout | ||
assert '"Carlos":"The writer of scripts."' in result.stdout | ||
assert '"Sebastian":"The type hints guy."' not in result.stdout | ||
|
||
|
||
def test_completion_greeter2(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, " "], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_TUTORIAL010.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "tutorial010.py --name Sebastian --greeter Carlos --greeter ", | ||
}, | ||
) | ||
assert '"Camila":"The reader of books."' in result.stdout | ||
assert '"Carlos":"The writer of scripts."' not in result.stdout | ||
assert '"Sebastian":"The type hints guy."' in result.stdout | ||
|
||
|
||
def test_1(): | ||
result = runner.invoke(mod.app, ["--name", "Camila", "--name", "Sebastian"]) | ||
assert result.exit_code == 0 | ||
assert "Hello Camila" in result.output | ||
assert "Hello Sebastian" in result.output | ||
|
||
|
||
def test_2(): | ||
result = runner.invoke( | ||
mod.app, ["--name", "Camila", "--name", "Sebastian", "--greeter", "Carlos"] | ||
) | ||
assert result.exit_code == 0 | ||
assert "Hello Camila, from Carlos" in result.output | ||
assert "Hello Sebastian, from Carlos" in result.output | ||
|
||
|
||
def test_3(): | ||
result = runner.invoke( | ||
mod.app, ["--name", "Camila", "--greeter", "Carlos", "--greeter", "Sebastian"] | ||
) | ||
assert result.exit_code == 0 | ||
assert "Hello Camila, from Carlos and Sebastian" 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 |
Oops, something went wrong.
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.
What was the rational to changing these existing 009 tests? I double checked and they succeed in their original form on
master
so I would opt to keep them as-is, to make it clear that there are no breaking changes introduced from this PR (which otherwise a changing test would alude to). If it's a matter of coverage, then I would try to incorporate that into the new 010 tests.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.
I believe my intent was to change this tutorial to using the context to get the parsed CLI parameters as the preferred method, but looking closer at the docs and the tutorials I added later I think this can be reverted and I can tweak some of the tutorials further down the page. I'll review that and make corrections.
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.
Reading closer I see what I was doing. I'm changing the "Getting the Context and the raw CLI parameters" tutorial to "Getting the Context and the Parsed CLI parameters". The reasoning for this is just to limit the size of the tutorials to keep it from becoming unwieldy. Instead of a second tutorial illustrating usage of args - which should probably be avoided in place of using the context we instead illustrate using the context and the parameter object to get prior parsed options.
If you agree with this I think all I should do here is make the section title change ("raw"->"parsed")
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.
Ok ok, ignore all previous comments. I think my change here was OBE by my addition of the Reusing Generic Completer Functions section. I can revert this tutorial to its prior state. It will, however, as with the first "raw CLI" parameter section require the args bug fix to be accurate.
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.
I'm a little confused as to what the conclusion is here (and what "OBE" means). I thought you'd said you'd revert the changes to this part of the tutorial, but they're still in the current PR diff. Also note that due to the added import in
tutorial009.py
and the code reformatting, the highlighted lines as referred to in the tutorial text won't be accurate anymore (but all of that is irrelevant if we do revert).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.
Sorry I forgot to leave a note about this. When I pulled #1134 out I did a run through of the tutorials to make sure everything still flowed well and made sense. There is one tutorial change and one additional tutorial that are part of this PR: