diff --git a/docs/contributing.md b/docs/contributing.md index 7600780afe..d7b7f53c8c 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -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). @@ -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/ diff --git a/docs/tutorial/options-autocompletion.md b/docs/tutorial/options-autocompletion.md index 43777d04fb..00106abf62 100644 --- a/docs/tutorial/options-autocompletion.md +++ b/docs/tutorial/options-autocompletion.md @@ -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+ @@ -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. diff --git a/pyproject.toml b/pyproject.toml index ce9d61afa3..e17f3628c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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', ] diff --git a/tests/assets/compat_click7_8.py b/tests/assets/compat_click7_8.py index 74ba9e1672..29539b5ae4 100644 --- a/tests/assets/compat_click7_8.py +++ b/tests/assets/compat_click7_8.py @@ -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__": diff --git a/tests/test_compat/test_option_get_help.py b/tests/test_compat/test_option_get_help.py index 362298b99a..343d2b16de 100644 --- a/tests/test_compat/test_option_get_help.py +++ b/tests/test_compat/test_option_get_help.py @@ -1,7 +1,3 @@ -import os -import subprocess -import sys - import typer.core from typer.testing import CliRunner @@ -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 diff --git a/typer/core.py b/typer/core.py index 00e21da869..aaa03b99cb 100644 --- a/typer/core.py +++ b/typer/core.py @@ -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"]: @@ -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], @@ -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], diff --git a/typer/models.py b/typer/models.py index 9bbe2a36d2..220dafa2f6 100644 --- a/typer/models.py +++ b/typer/models.py @@ -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], @@ -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], @@ -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], diff --git a/typer/params.py b/typer/params.py index 2fd025c90d..7c84bb2383 100644 --- a/typer/params.py +++ b/typer/params.py @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], @@ -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],