Skip to content

Commit d8e56e2

Browse files
svlandegpre-commit-ci[bot]tiangolo
authored
🗑️ Deprecate shell_complete and continue to use autocompletion for CLI parameters (#974)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sebastián RamĂ­rez <[email protected]>
1 parent 5f378ee commit d8e56e2

File tree

7 files changed

+38
-35
lines changed

7 files changed

+38
-35
lines changed

‎docs/tutorial/options-autocompletion.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,13 @@ You can declare function parameters of these types:
384384
* `List[str]`: for the raw *CLI parameters*.
385385

386386
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**" ✨
387+
388+
## Comparison to Click functionality
389+
390+
Note that Click 7 had a similar [`autocompletion` function](https://click.palletsprojects.com/en/7.x/bashcomplete/), but it worked slightly differently.
391+
392+
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.
393+
394+
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.
395+
396+
However, Typer continues to use the `autocompletion` functionality as described on this page.

‎pyproject.toml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ xfail_strict = true
115115
junit_family = "xunit2"
116116
filterwarnings = [
117117
"error",
118-
# TODO: until I refactor completion to use the new shell_complete
119-
"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",
120118
# For pytest-xdist
121119
'ignore::DeprecationWarning:xdist',
122120
]

‎tests/assets/compat_click7_8.py‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
1-
from typing import List
2-
3-
import click
41
import typer
52

63
app = typer.Typer()
74

85

9-
def shell_complete(
10-
ctx: click.Context, param: click.Parameter, incomplete: str
11-
) -> List[str]:
12-
return ["Jonny"]
13-
14-
156
@app.command(context_settings={"auto_envvar_prefix": "TEST"})
167
def main(
178
name: str = typer.Option("John", hidden=True),
189
lastname: str = typer.Option("Doe", "/lastname", show_default="Mr. Doe"),
1910
age: int = typer.Option(lambda: 42, show_default=True),
20-
nickname: str = typer.Option("", shell_complete=shell_complete),
2111
):
2212
"""
2313
Say hello.
2414
"""
25-
print(f"Hello {name} {lastname}, it seems you have {age}, {nickname}")
15+
print(f"Hello {name} {lastname}, it seems you have {age}")
2616

2717

2818
if __name__ == "__main__":

‎tests/test_compat/test_option_get_help.py‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import os
2-
import subprocess
3-
import sys
4-
51
import typer.core
62
from typer.testing import CliRunner
73

@@ -37,17 +33,3 @@ def test_coverage_call():
3733
result = runner.invoke(mod.app)
3834
assert result.exit_code == 0
3935
assert "Hello John Doe, it seems you have 42" in result.output
40-
41-
42-
def 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

‎typer/core.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,18 @@ def _typer_param_setup_autocompletion_compat(
6262
Callable[[click.Context, List[str], str], List[Union[Tuple[str, str], str]]]
6363
] = None,
6464
) -> None:
65-
if autocompletion is not None and self._custom_shell_complete is None:
65+
if self._custom_shell_complete is not None:
6666
import warnings
6767

6868
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. ",
7271
DeprecationWarning,
7372
stacklevel=2,
7473
)
7574

75+
if autocompletion is not None:
76+
7677
def compat_autocompletion(
7778
ctx: click.Context, param: click.core.Parameter, incomplete: str
7879
) -> List["click.shell_completion.CompletionItem"]:
@@ -267,6 +268,8 @@ def __init__(
267268
expose_value: bool = True,
268269
is_eager: bool = False,
269270
envvar: Optional[Union[str, List[str]]] = None,
271+
# Note that shell_complete is not fully supported and will be removed in future versions
272+
# TODO: Remove shell_complete in a future version (after 0.16.0)
270273
shell_complete: Optional[
271274
Callable[
272275
[click.Context, click.Parameter, str],
@@ -406,6 +409,8 @@ def __init__(
406409
expose_value: bool = True,
407410
is_eager: bool = False,
408411
envvar: Optional[Union[str, List[str]]] = None,
412+
# Note that shell_complete is not fully supported and will be removed in future versions
413+
# TODO: Remove shell_complete in a future version (after 0.16.0)
409414
shell_complete: Optional[
410415
Callable[
411416
[click.Context, click.Parameter, str],

‎typer/models.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ def __init__(
173173
expose_value: bool = True,
174174
is_eager: bool = False,
175175
envvar: Optional[Union[str, List[str]]] = None,
176+
# Note that shell_complete is not fully supported and will be removed in future versions
177+
# TODO: Remove shell_complete in a future version (after 0.16.0)
176178
shell_complete: Optional[
177179
Callable[
178180
[click.Context, click.Parameter, str],
@@ -281,6 +283,8 @@ def __init__(
281283
expose_value: bool = True,
282284
is_eager: bool = False,
283285
envvar: Optional[Union[str, List[str]]] = None,
286+
# Note that shell_complete is not fully supported and will be removed in future versions
287+
# TODO: Remove shell_complete in a future version (after 0.16.0)
284288
shell_complete: Optional[
285289
Callable[
286290
[click.Context, click.Parameter, str],
@@ -408,6 +412,8 @@ def __init__(
408412
expose_value: bool = True,
409413
is_eager: bool = False,
410414
envvar: Optional[Union[str, List[str]]] = None,
415+
# Note that shell_complete is not fully supported and will be removed in future versions
416+
# TODO: Remove shell_complete in a future version (after 0.16.0)
411417
shell_complete: Optional[
412418
Callable[
413419
[click.Context, click.Parameter, str],

‎typer/params.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def Option(
1919
expose_value: bool = True,
2020
is_eager: bool = False,
2121
envvar: Optional[Union[str, List[str]]] = None,
22+
# Note that shell_complete is not fully supported and will be removed in future versions
23+
# TODO: Remove shell_complete in a future version (after 0.16.0)
2224
shell_complete: Optional[
2325
Callable[
2426
[click.Context, click.Parameter, str],
@@ -83,6 +85,8 @@ def Option(
8385
expose_value: bool = True,
8486
is_eager: bool = False,
8587
envvar: Optional[Union[str, List[str]]] = None,
88+
# Note that shell_complete is not fully supported and will be removed in future versions
89+
# TODO: Remove shell_complete in a future version (after 0.16.0)
8690
shell_complete: Optional[
8791
Callable[
8892
[click.Context, click.Parameter, str],
@@ -145,6 +149,8 @@ def Option(
145149
expose_value: bool = True,
146150
is_eager: bool = False,
147151
envvar: Optional[Union[str, List[str]]] = None,
152+
# Note that shell_complete is not fully supported and will be removed in future versions
153+
# TODO: Remove shell_complete in a future version (after 0.16.0)
148154
shell_complete: Optional[
149155
Callable[
150156
[click.Context, click.Parameter, str],
@@ -265,6 +271,8 @@ def Argument(
265271
expose_value: bool = True,
266272
is_eager: bool = False,
267273
envvar: Optional[Union[str, List[str]]] = None,
274+
# Note that shell_complete is not fully supported and will be removed in future versions
275+
# TODO: Remove shell_complete in a future version (after 0.16.0)
268276
shell_complete: Optional[
269277
Callable[
270278
[click.Context, click.Parameter, str],
@@ -320,6 +328,8 @@ def Argument(
320328
expose_value: bool = True,
321329
is_eager: bool = False,
322330
envvar: Optional[Union[str, List[str]]] = None,
331+
# Note that shell_complete is not fully supported and will be removed in future versions
332+
# TODO: Remove shell_complete in a future version (after 0.16.0)
323333
shell_complete: Optional[
324334
Callable[
325335
[click.Context, click.Parameter, str],
@@ -373,6 +383,8 @@ def Argument(
373383
expose_value: bool = True,
374384
is_eager: bool = False,
375385
envvar: Optional[Union[str, List[str]]] = None,
386+
# Note that shell_complete is not fully supported and will be removed in future versions
387+
# TODO: Remove shell_complete in a future version (after 0.16.0)
376388
shell_complete: Optional[
377389
Callable[
378390
[click.Context, click.Parameter, str],

0 commit comments

Comments
 (0)