From 698d85e9b93eb5bc86fd21e5515695e23f79aca6 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 4 Sep 2024 14:36:11 +0200 Subject: [PATCH 01/10] fix typos --- docs/contributing.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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/ From f47750f268d0f8c291e6e1245ba73d4e4da9686c Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 4 Sep 2024 16:48:38 +0200 Subject: [PATCH 02/10] fix typo --- docs/tutorial/options-autocompletion.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial/options-autocompletion.md b/docs/tutorial/options-autocompletion.md index 43777d04fb..93c59ff64f 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+ From 117319238a7578feb6104d6d0fe27df016b078e1 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 5 Sep 2024 11:52:59 +0200 Subject: [PATCH 03/10] update warning to deprecate shell_complete --- pyproject.toml | 2 -- typer/core.py | 8 +++----- 2 files changed, 3 insertions(+), 7 deletions(-) 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/typer/core.py b/typer/core.py index 00e21da869..e070a1c412 100644 --- a/typer/core.py +++ b/typer/core.py @@ -62,13 +62,11 @@ 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 autocompletion is None and 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 usage of 'shell_complete' will be deprecated in upcoming versions. ", DeprecationWarning, stacklevel=2, ) From 1975fce8268b18f550aa4bc5a722cd9e7910d1e2 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 5 Sep 2024 11:53:29 +0200 Subject: [PATCH 04/10] add notes in the source code that shell_complete is not supported and will be removed --- typer/core.py | 2 ++ typer/models.py | 3 +++ typer/params.py | 6 ++++++ 3 files changed, 11 insertions(+) diff --git a/typer/core.py b/typer/core.py index e070a1c412..b6961371d3 100644 --- a/typer/core.py +++ b/typer/core.py @@ -263,6 +263,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], @@ -397,6 +398,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], From cfbeb3d0ad073c63a1d056ad6c2fb35c4072cc0e Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 5 Sep 2024 11:54:04 +0200 Subject: [PATCH 05/10] add note in the documentation about comparison to Click functionality --- docs/tutorial/options-autocompletion.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/tutorial/options-autocompletion.md b/docs/tutorial/options-autocompletion.md index 93c59ff64f..28842aadc3 100644 --- a/docs/tutorial/options-autocompletion.md +++ b/docs/tutorial/options-autocompletion.md @@ -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 keyword 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. From 749cadef5b929a7f30dc896c7e65503ac8e6d1a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Sep 2024 10:01:54 +0000 Subject: [PATCH 06/10] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/tutorial/options-autocompletion.md | 2 +- typer/core.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/tutorial/options-autocompletion.md b/docs/tutorial/options-autocompletion.md index 28842aadc3..c41ecce9dd 100644 --- a/docs/tutorial/options-autocompletion.md +++ b/docs/tutorial/options-autocompletion.md @@ -567,7 +567,7 @@ It doesn't matter how you name them, in which order, or which ones of the 3 opti ## 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. +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 keyword arguments `ctx`, `args` and `incomplete` in that exact order, instead of matching them dynamically based on types, as Typer does. diff --git a/typer/core.py b/typer/core.py index b6961371d3..d98ece5c7e 100644 --- a/typer/core.py +++ b/typer/core.py @@ -64,6 +64,7 @@ def _typer_param_setup_autocompletion_compat( ) -> None: if autocompletion is None and self._custom_shell_complete is not None: import warnings + warnings.warn( "In Typer, only the parameter 'autocompletion' is supported. " "The usage of 'shell_complete' will be deprecated in upcoming versions. ", From 286b94eff7feae06a9bdd4914ec91baf3b6e51bb Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 5 Sep 2024 14:11:04 +0200 Subject: [PATCH 07/10] fix if conditions --- typer/core.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/typer/core.py b/typer/core.py index d98ece5c7e..27db7c72c3 100644 --- a/typer/core.py +++ b/typer/core.py @@ -62,7 +62,7 @@ def _typer_param_setup_autocompletion_compat( Callable[[click.Context, List[str], str], List[Union[Tuple[str, str], str]]] ] = None, ) -> None: - if autocompletion is None and self._custom_shell_complete is not None: + if self._custom_shell_complete is not None: import warnings warnings.warn( @@ -72,6 +72,8 @@ def _typer_param_setup_autocompletion_compat( stacklevel=2, ) + if autocompletion is not None: + def compat_autocompletion( ctx: click.Context, param: click.core.Parameter, incomplete: str ) -> List["click.shell_completion.CompletionItem"]: From ca4ca2c1740bf8b42978a8741682e3dc36045f84 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 5 Sep 2024 14:38:38 +0200 Subject: [PATCH 08/10] remove test with shell_complete in Option --- tests/assets/compat_click7_8.py | 12 +----------- tests/test_compat/test_option_get_help.py | 18 ------------------ 2 files changed, 1 insertion(+), 29 deletions(-) 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 From 2261391a7c5cd0ec416fb8d65a7390b81144cc68 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 5 Sep 2024 14:51:17 +0200 Subject: [PATCH 09/10] rephrase slightly --- typer/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/core.py b/typer/core.py index 27db7c72c3..aaa03b99cb 100644 --- a/typer/core.py +++ b/typer/core.py @@ -67,7 +67,7 @@ def _typer_param_setup_autocompletion_compat( warnings.warn( "In Typer, only the parameter 'autocompletion' is supported. " - "The usage of 'shell_complete' will be deprecated in upcoming versions. ", + "The support for 'shell_complete' is deprecated and will be removed in upcoming versions. ", DeprecationWarning, stacklevel=2, ) From e17a0f56bd05effcaaafcec6263fddd84d3a4098 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 5 Sep 2024 14:55:31 +0200 Subject: [PATCH 10/10] fix --- docs/tutorial/options-autocompletion.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial/options-autocompletion.md b/docs/tutorial/options-autocompletion.md index c41ecce9dd..00106abf62 100644 --- a/docs/tutorial/options-autocompletion.md +++ b/docs/tutorial/options-autocompletion.md @@ -569,7 +569,7 @@ It doesn't matter how you name them, in which order, or which ones of the 3 opti 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 keyword arguments `ctx`, `args` and `incomplete` in that exact order, instead of matching them dynamically based on types, as Typer does. +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.