Skip to content
Myr

🗑️ Deprecate `shell_complete` and continue to use `autocompletion` for CLI parameters (#974)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sofie Van Landeghem · committed by GitHub · parent 5f378ee · Browse files · 7 files+38 added−35 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>

Diff stats

7 files+38 added−35 removed

  • tests:+1 added−29 removed
  • other:+37 added−6 removed

7 files+38 added−35 removed

modified:docs/tutorial/options-autocompletion.md+10 added
382 unmodified lines
383* `typer.Context`: for the current context.384* `List[str]`: for the raw *CLI parameters*.385386It doesn't matter how you name them, in which order, or which ones of the 3 options you declare. It will all "**just work**" ✨387388## Comparison to Click functionality389390Note that Click 7 had a similar [`autocompletion` function](https://click.palletsprojects.com/en/7.x/bashcomplete/), but it worked slightly differently.391392It 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.393394Since 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.395396However, Typer continues to use the `autocompletion` functionality as described on this page.
modified:tests/assets/compat_click7_8.py+1 added-11 removed
1from typing import List23import click1import typer23app = typer.Typer()459def shell_complete(10 ctx: click.Context, param: click.Parameter, incomplete: str11) -> List[str]:12 return ["Jonny"]13146@app.command(context_settings={"auto_envvar_prefix": "TEST"})7def main(8 name: str = typer.Option("John", hidden=True),9 lastname: str = typer.Option("Doe", "/lastname", show_default="Mr. Doe"),10 age: int = typer.Option(lambda: 42, show_default=True),20 nickname: str = typer.Option("", shell_complete=shell_complete),11):12 """13 Say hello.14 """25 print(f"Hello {name} {lastname}, it seems you have {age}, {nickname}")15 print(f"Hello {name} {lastname}, it seems you have {age}")161718if __name__ == "__main__":19 app()
modified:tests/test_compat/test_option_get_help.py-18 removed
1import os2import subprocess3import sys41import typer.core2from typer.testing import CliRunner34from tests.assets import compat_click7_8 as mod
27 unmodified lines
32def test_coverage_call():33 result = runner.invoke(mod.app)34 assert result.exit_code == 035 assert "Hello John Doe, it seems you have 42" in result.output404142def test_completion():43 result = subprocess.run(44 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],45 capture_output=True,46 encoding="utf-8",47 env={48 **os.environ,49 "_COMPAT_CLICK7_8.PY_COMPLETE": "complete_zsh",50 "_TYPER_COMPLETE_ARGS": "compat_click7_8.py --nickname ",51 },52 )53 assert "Jonny" in result.stdout
modified:typer/core.py+9 added-4 removed
60 unmodified lines
61 autocompletion: Optional[62 Callable[[click.Context, List[str], str], List[Union[Tuple[str, str], str]]]63 ] = None,64) -> None:65 if autocompletion is not None and self._custom_shell_complete is None:65 if self._custom_shell_complete is not None:66 import warnings6768 warnings.warn(69 "'autocompletion' is renamed to 'shell_complete'. The old name is"70 " deprecated and will be removed in Click 8.1. See the docs about"71 " 'Parameter' for information about new behavior.",69 "In Typer, only the parameter 'autocompletion' is supported. "70 "The support for 'shell_complete' is deprecated and will be removed in upcoming versions. ",71 DeprecationWarning,72 stacklevel=2,73 )7475 if autocompletion is not None:7677 def compat_autocompletion(78 ctx: click.Context, param: click.core.Parameter, incomplete: str79 ) -> List["click.shell_completion.CompletionItem"]:80 from click.shell_completion import CompletionItem
186 unmodified lines
267 metavar: Optional[str] = None,268 expose_value: bool = True,269 is_eager: bool = False,270 envvar: Optional[Union[str, List[str]]] = None,271 # Note that shell_complete is not fully supported and will be removed in future versions272 # TODO: Remove shell_complete in a future version (after 0.16.0)273 shell_complete: Optional[274 Callable[275 [click.Context, click.Parameter, str],276 Union[List["click.shell_completion.CompletionItem"], List[str]],
131 unmodified lines
408 metavar: Optional[str] = None,409 expose_value: bool = True,410 is_eager: bool = False,411 envvar: Optional[Union[str, List[str]]] = None,412 # Note that shell_complete is not fully supported and will be removed in future versions413 # TODO: Remove shell_complete in a future version (after 0.16.0)414 shell_complete: Optional[415 Callable[416 [click.Context, click.Parameter, str],417 Union[List["click.shell_completion.CompletionItem"], List[str]],
350 unmodified lines
modified:typer/models.py+6 added
171 unmodified lines
172 metavar: Optional[str] = None,173 expose_value: bool = True,174 is_eager: bool = False,175 envvar: Optional[Union[str, List[str]]] = None,176 # Note that shell_complete is not fully supported and will be removed in future versions177 # TODO: Remove shell_complete in a future version (after 0.16.0)178 shell_complete: Optional[179 Callable[180 [click.Context, click.Parameter, str],181 Union[List["click.shell_completion.CompletionItem"], List[str]],
100 unmodified lines
282 metavar: Optional[str] = None,283 expose_value: bool = True,284 is_eager: bool = False,285 envvar: Optional[Union[str, List[str]]] = None,286 # Note that shell_complete is not fully supported and will be removed in future versions287 # TODO: Remove shell_complete in a future version (after 0.16.0)288 shell_complete: Optional[289 Callable[290 [click.Context, click.Parameter, str],291 Union[List["click.shell_completion.CompletionItem"], List[str]],
119 unmodified lines
411 metavar: Optional[str] = None,412 expose_value: bool = True,413 is_eager: bool = False,414 envvar: Optional[Union[str, List[str]]] = None,415 # Note that shell_complete is not fully supported and will be removed in future versions416 # TODO: Remove shell_complete in a future version (after 0.16.0)417 shell_complete: Optional[418 Callable[419 [click.Context, click.Parameter, str],420 Union[List["click.shell_completion.CompletionItem"], List[str]],
113 unmodified lines
modified:typer/params.py+12 added
17 unmodified lines
18 metavar: Optional[str] = None,19 expose_value: bool = True,20 is_eager: bool = False,21 envvar: Optional[Union[str, List[str]]] = None,22 # Note that shell_complete is not fully supported and will be removed in future versions23 # TODO: Remove shell_complete in a future version (after 0.16.0)24 shell_complete: Optional[25 Callable[26 [click.Context, click.Parameter, str],27 Union[List["click.shell_completion.CompletionItem"], List[str]],
56 unmodified lines
84 metavar: Optional[str] = None,85 expose_value: bool = True,86 is_eager: bool = False,87 envvar: Optional[Union[str, List[str]]] = None,88 # Note that shell_complete is not fully supported and will be removed in future versions89 # TODO: Remove shell_complete in a future version (after 0.16.0)90 shell_complete: Optional[91 Callable[92 [click.Context, click.Parameter, str],93 Union[List["click.shell_completion.CompletionItem"], List[str]],
54 unmodified lines
148 metavar: Optional[str] = None,149 expose_value: bool = True,150 is_eager: bool = False,151 envvar: Optional[Union[str, List[str]]] = None,152 # Note that shell_complete is not fully supported and will be removed in future versions153 # TODO: Remove shell_complete in a future version (after 0.16.0)154 shell_complete: Optional[155 Callable[156 [click.Context, click.Parameter, str],157 Union[List["click.shell_completion.CompletionItem"], List[str]],
112 unmodified lines
270 metavar: Optional[str] = None,271 expose_value: bool = True,272 is_eager: bool = False,273 envvar: Optional[Union[str, List[str]]] = None,274 # Note that shell_complete is not fully supported and will be removed in future versions275 # TODO: Remove shell_complete in a future version (after 0.16.0)276 shell_complete: Optional[277 Callable[278 [click.Context, click.Parameter, str],279 Union[List["click.shell_completion.CompletionItem"], List[str]],
47 unmodified lines
327 metavar: Optional[str] = None,328 expose_value: bool = True,329 is_eager: bool = False,330 envvar: Optional[Union[str, List[str]]] = None,331 # Note that shell_complete is not fully supported and will be removed in future versions332 # TODO: Remove shell_complete in a future version (after 0.16.0)333 shell_complete: Optional[334 Callable[335 [click.Context, click.Parameter, str],336 Union[List["click.shell_completion.CompletionItem"], List[str]],
45 unmodified lines
382 metavar: Optional[str] = None,383 expose_value: bool = True,384 is_eager: bool = False,385 envvar: Optional[Union[str, List[str]]] = None,386 # Note that shell_complete is not fully supported and will be removed in future versions387 # TODO: Remove shell_complete in a future version (after 0.16.0)388 shell_complete: Optional[389 Callable[390 [click.Context, click.Parameter, str],391 Union[List["click.shell_completion.CompletionItem"], List[str]],
88 unmodified lines
modified:pyproject.toml-2 removed
113 unmodified lines
114xfail_strict = true115junit_family = "xunit2"116filterwarnings = [117 "error",118 # TODO: until I refactor completion to use the new shell_complete119 "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",118 # For pytest-xdist119 'ignore::DeprecationWarning:xdist',120]121
81 unmodified lines