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

🐛 Fix shell_complete not working for Arguments #737

Merged
merged 13 commits into from
Aug 16, 2024
22 changes: 22 additions & 0 deletions tests/assets/compat_arg_complete_click7_8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import click
import typer

app = typer.Typer()


def shell_complete(ctx: click.Context, param: click.Parameter, incomplete: str):
bckohan marked this conversation as resolved.
Show resolved Hide resolved
typer.echo(f"ctx: {ctx.info_name}", err=True)
typer.echo(f"arg is: {param.name}", err=True)
typer.echo(f"incomplete is: {incomplete}", err=True)
return ["Emma"]


@app.command(context_settings={"auto_envvar_prefix": "TEST"})
def main(name: str = typer.Argument(shell_complete=shell_complete)):
"""
Say hello.
"""


if __name__ == "__main__":
app()
28 changes: 28 additions & 0 deletions tests/test_compat/test_arg_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import subprocess
import sys

from typer._compat_utils import _get_click_major

from tests.assets import compat_arg_complete_click7_8 as mod


def test_arg_completion():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, "E"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_COMPAT_ARG_COMPLETE_CLICK7_8.PY_COMPLETE": "complete_zsh",
"_TYPER_COMPLETE_ARGS": "compat_arg_complete_click7_8.py E",
"_TYPER_COMPLETE_TESTING": "True",
},
)
# TODO: when deprecating Click 7, remove second option
assert "Emma" in result.stdout or "_files" in result.stdout
if _get_click_major() > 7:
assert "ctx: compat_arg_complete_click7_8" in result.stderr
assert "arg is: name" in result.stderr
assert "incomplete is: E" in result.stderr
1 change: 1 addition & 0 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ def get_click_param(
expose_value=parameter_info.expose_value,
is_eager=parameter_info.is_eager,
envvar=parameter_info.envvar,
shell_complete=parameter_info.shell_complete,
svlandeg marked this conversation as resolved.
Show resolved Hide resolved
autocompletion=get_param_completion(parameter_info.autocompletion),
# Rich settings
rich_help_panel=parameter_info.rich_help_panel,
Expand Down
Loading