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

🗑️ Deprecate shell_complete and continue to use autocompletion #974

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ This command generates a directory `./htmlcov/`, if you open the file `./htmlcov

To try and test the completion for different shells and check that they are working you can use a Docker container.

There's a `Dockerfile` and a a Docker Compose file `compose.yaml` at `./scripts/docker/`.
There's a `Dockerfile` and a Docker Compose file `compose.yaml` at `./scripts/docker/`.

It has installed `bash`, `zsh`, `fish`, and `pwsh` (PowerShell for Linux).

Expand Down Expand Up @@ -237,9 +237,9 @@ That way, you can edit the documentation/source files and see the changes live.

/// tip

Alternatively, you can perform the same steps that scripts does manually.
Alternatively, you can perform the same steps that script does manually.

Go into the docs director at `docs/`:
Go into the docs directory at `docs/`:

```console
$ cd docs/
Expand Down
12 changes: 11 additions & 1 deletion docs/tutorial/options-autocompletion.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ After installing completion for your own Python package (or using the `typer` co

To check it quickly without creating a new Python package, use the `typer` command.

Then let's create small example program:
Then let's create a small example program:

//// tab | Python 3.7+

Expand Down Expand Up @@ -564,3 +564,13 @@ You can declare function parameters of these types:
* `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**" ✨

## Comparison to Click functionality

Note that Click 7 had a similar [`autocompletion` function](https://click.palletsprojects.com/en/7.x/bashcomplete/), but it worked slightly differently.

It required the callback function to take exactly the 3 arguments `ctx`, `args` and `incomplete` in that exact order, instead of matching them dynamically based on types, as Typer does.

Since Click 8, this functionality has been replaced by [`shell_complete`](https://click.palletsprojects.com/en/8.1.x/api/#click.ParamType.shell_complete), which still depends on the exact order of arguments for the callback function.

However, Typer continues to use the `autocompletion` functionality as described on this page.
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ xfail_strict = true
junit_family = "xunit2"
filterwarnings = [
"error",
# TODO: until I refactor completion to use the new shell_complete
"ignore:'autocompletion' is renamed to 'shell_complete'. The old name is deprecated and will be removed in Click 8.1. See the docs about 'Parameter' for information about new behavior.:DeprecationWarning:typer",
# For pytest-xdist
'ignore::DeprecationWarning:xdist',
]
Expand Down
12 changes: 1 addition & 11 deletions tests/assets/compat_click7_8.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
from typing import List

import click
import typer

app = typer.Typer()


def shell_complete(
ctx: click.Context, param: click.Parameter, incomplete: str
) -> List[str]:
return ["Jonny"]


@app.command(context_settings={"auto_envvar_prefix": "TEST"})
def main(
name: str = typer.Option("John", hidden=True),
lastname: str = typer.Option("Doe", "/lastname", show_default="Mr. Doe"),
age: int = typer.Option(lambda: 42, show_default=True),
nickname: str = typer.Option("", shell_complete=shell_complete),
):
"""
Say hello.
"""
print(f"Hello {name} {lastname}, it seems you have {age}, {nickname}")
print(f"Hello {name} {lastname}, it seems you have {age}")


if __name__ == "__main__":
Expand Down
18 changes: 0 additions & 18 deletions tests/test_compat/test_option_get_help.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import os
import subprocess
import sys

import typer.core
from typer.testing import CliRunner

Expand Down Expand Up @@ -37,17 +33,3 @@ def test_coverage_call():
result = runner.invoke(mod.app)
assert result.exit_code == 0
assert "Hello John Doe, it seems you have 42" in result.output


def test_completion():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_COMPAT_CLICK7_8.PY_COMPLETE": "complete_zsh",
"_TYPER_COMPLETE_ARGS": "compat_click7_8.py --nickname ",
},
)
assert "Jonny" in result.stdout
11 changes: 7 additions & 4 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,18 @@ def _typer_param_setup_autocompletion_compat(
Callable[[click.Context, List[str], str], List[Union[Tuple[str, str], str]]]
] = None,
) -> None:
if autocompletion is not None and self._custom_shell_complete is None:
if self._custom_shell_complete is not None:
import warnings

warnings.warn(
"'autocompletion' is renamed to 'shell_complete'. The old name is"
" deprecated and will be removed in Click 8.1. See the docs about"
" 'Parameter' for information about new behavior.",
"In Typer, only the parameter 'autocompletion' is supported. "
"The support for 'shell_complete' is deprecated and will be removed in upcoming versions. ",
DeprecationWarning,
stacklevel=2,
)

if autocompletion is not None:

def compat_autocompletion(
ctx: click.Context, param: click.core.Parameter, incomplete: str
) -> List["click.shell_completion.CompletionItem"]:
Expand Down Expand Up @@ -265,6 +266,7 @@ def __init__(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down Expand Up @@ -399,6 +401,7 @@ def __init__(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down
3 changes: 3 additions & 0 deletions typer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def __init__(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down Expand Up @@ -281,6 +282,7 @@ def __init__(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down Expand Up @@ -400,6 +402,7 @@ def __init__(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down
6 changes: 6 additions & 0 deletions typer/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def Option(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down Expand Up @@ -82,6 +83,7 @@ def Option(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down Expand Up @@ -143,6 +145,7 @@ def Option(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down Expand Up @@ -262,6 +265,7 @@ def Argument(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down Expand Up @@ -317,6 +321,7 @@ def Argument(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down Expand Up @@ -370,6 +375,7 @@ def Argument(
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
# Note that shell_complete is not fully supported and will be removed in future versions
shell_complete: Optional[
Callable[
[click.Context, click.Parameter, str],
Expand Down
Loading