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

✨ Upgrade autocompletion functionality for compatibility with shell_complete #1006

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion docs/tutorial/options-autocompletion.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Hello Sebastian

And the same way as before, we want to provide **completion** for those names. But we don't want to provide the **same names** for completion if they were already given in previous parameters.

For that, we will access and use the "Context". When you create a **Typer** application it uses Click underneath. And every Click application has a special object called a <a href="https://click.palletsprojects.com/en/7.x/commands/#nested-handling-and-contexts" class="external-link" target="_blank">"Context"</a> that is normally hidden.
For that, we will access and use the "Context". When you create a **Typer** application it uses Click underneath. And every Click application has a special object called a <a href="https://click.palletsprojects.com/en/stable/commands/#nested-handling-and-contexts" class="external-link" target="_blank">"Context"</a> that is normally hidden.

But you can access the context by declaring a function parameter of type `typer.Context`.

Expand Down Expand Up @@ -264,6 +264,36 @@ It's quite possible that if there's only one option left, your shell will comple

///

## Reusing generic completer functions

You may want to reuse completer functions across CLI applications or within the same CLI application. If you need to filter out previously supplied parameters the completer function will first have to determine which parameter it is being asked to complete.

We can declare a parameter of type <a href="https://click.palletsprojects.com/en/stable/api/#click.Parameter" class="external-link" target="_blank">click.Parameter</a> along with the `click.Context` in our completer function to determine this. For example, lets revisit our above context example where we filter out duplicates but add a second greeter argument that reuses the same completer function:

{* docs_src/options_autocompletion/tutorial010_an.py hl[15:16] *}

/// tip

You may also return <a href="https://click.palletsprojects.com/en/stable/api/#click.shell_completion.CompletionItem" class="external-link" target="_blank">click.shell_completion.CompletionItem</a> objects from completer functions instead of 2-tuples.

///


Check it:

<div class="termy">

```console
$ typer ./main.py run --name Sebastian --greeter Camila --greeter [TAB][TAB]

// Our function returns Sebastian too because it is completing greeter
Carlos -- The writer of scripts.
Sebastian -- The type hints guy.
```

</div>


## Getting the raw *CLI parameters*

You can also get the raw *CLI parameters*, just a `list` of `str` with everything passed in the command line before the incomplete value.
Expand Down Expand Up @@ -381,6 +411,7 @@ You can declare function parameters of these types:

* `str`: for the incomplete value.
* `typer.Context`: for the current context.
* `click.Parameter`: for the CLI parameter being completed.
* `List[str]`: for the raw *CLI parameters*.

It doesn't matter how you name them, in which order, or which ones of the 3 options you declare. It will all "**just work**" ✨
Expand Down
6 changes: 3 additions & 3 deletions docs_src/options_autocompletion/tutorial009.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List

import typer
from click.core import Parameter
from rich.console import Console

valid_completion_items = [
Expand All @@ -12,9 +13,8 @@
err_console = Console(stderr=True)


def complete_name(ctx: typer.Context, args: List[str], incomplete: str):
err_console.print(f"{args}")
names = ctx.params.get("name") or []
def complete_name(ctx: typer.Context, param: Parameter, incomplete: str):
names = ctx.params.get(param.name) or []
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@bckohan bckohan Jan 24, 2025

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")

Copy link
Contributor Author

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.

Copy link
Member

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).

Copy link
Contributor Author

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:

  1. In main, tutorial009 is the last tutorial and the point of it is to illustrate that you can receive all the arguments if you would like. Since we're adding param as another argument I simply extended this tutorial to show that you can get all 4.
  2. tutorial010 is new and illustrates using the param object to determine which parameter the generic completer function is being asked to complete.
  • OBE == Overcome By Events

for name, help_text in valid_completion_items:
if name.startswith(incomplete) and name not in names:
yield (name, help_text)
Expand Down
6 changes: 3 additions & 3 deletions docs_src/options_autocompletion/tutorial009_an.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List

import typer
from click.core import Parameter
from rich.console import Console
from typing_extensions import Annotated

Expand All @@ -13,9 +14,8 @@
err_console = Console(stderr=True)


def complete_name(ctx: typer.Context, args: List[str], incomplete: str):
err_console.print(f"{args}")
names = ctx.params.get("name") or []
def complete_name(ctx: typer.Context, param: Parameter, incomplete: str):
names = ctx.params.get(param.name) or []
for name, help_text in valid_completion_items:
if name.startswith(incomplete) and name not in names:
yield (name, help_text)
Expand Down
38 changes: 38 additions & 0 deletions docs_src/options_autocompletion/tutorial010.py
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()
41 changes: 41 additions & 0 deletions docs_src/options_autocompletion/tutorial010_an.py
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()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ ignore = [
"docs_src/options_autocompletion/tutorial007_an.py" = ["B006"]
"docs_src/options_autocompletion/tutorial008_an.py" = ["B006"]
"docs_src/options_autocompletion/tutorial009_an.py" = ["B006"]
"docs_src/options_autocompletion/tutorial010_an.py" = ["B006"]
"docs_src/parameter_types/enum/tutorial003_an.py" = ["B006"]
# Loop control variable `value` not used within loop body
"docs_src/progressbar/tutorial001.py" = ["B007"]
Expand Down
3 changes: 2 additions & 1 deletion tests/assets/completion_no_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
app = typer.Typer()


def complete(ctx, args, incomplete):
def complete(ctx, args, param, incomplete):
typer.echo(f"info name is: {ctx.info_name}", err=True)
typer.echo(f"args is: {args}", err=True)
typer.echo(f"param is: {param.name}", err=True)
typer.echo(f"incomplete is: {incomplete}", err=True)
return [
("Camila", "The reader of books."),
Expand Down
3 changes: 2 additions & 1 deletion tests/assets/completion_no_types_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
app = typer.Typer()


def complete(args, incomplete, ctx):
def complete(args, incomplete, ctx, param):
typer.echo(f"info name is: {ctx.info_name}", err=True)
typer.echo(f"args is: {args}", err=True)
typer.echo(f"param is: {param.name}", err=True)
typer.echo(f"incomplete is: {incomplete}", err=True)
return [
("Camila", "The reader of books."),
Expand Down
6 changes: 4 additions & 2 deletions tests/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ def test_completion_untyped_parameters():
},
)
assert "info name is: completion_no_types.py" in result.stderr
assert "args is: []" in result.stderr
assert "args is: ['--name', 'Sebastian', '--name']" in result.stderr
assert "param is: name" in result.stderr
assert "incomplete is: Ca" in result.stderr
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
Expand All @@ -202,7 +203,8 @@ def test_completion_untyped_parameters_different_order_correct_names():
},
)
assert "info name is: completion_no_types_order.py" in result.stderr
assert "args is: []" in result.stderr
assert "args is: ['--name', 'Sebastian', '--name']" in result.stderr
assert "param is: name" in result.stderr
assert "incomplete is: Ca" in result.stderr
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_completion():
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
assert '"Sebastian":"The type hints guy."' in result.stdout
assert "[]" in result.stderr
assert "--name" in result.stderr


def test_1():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_completion():
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
assert '"Sebastian":"The type hints guy."' in result.stdout
assert "[]" in result.stderr
assert "--name" in result.stderr


def test_1():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def test_completion():
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
assert "[]" in result.stderr


def test_1():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def test_completion():
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
assert "[]" in result.stderr


def test_1():
Expand Down
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
Loading
Loading