Skip to content

Commit

Permalink
🐛 Fix shell_complete not working for Arguments (#737)
Browse files Browse the repository at this point in the history
Co-authored-by: svlandeg <svlandeg@github.com>
  • Loading branch information
bckohan and svlandeg authored Aug 16, 2024
1 parent 76ca3e3 commit dcb45b1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/assets/completion_argument.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):
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()
19 changes: 19 additions & 0 deletions tests/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ def main(name: str = typer.Option(..., callback=name_callback)):
assert "value is: Camila" in result.stdout


def test_completion_argument():
file_path = Path(__file__).parent / "assets/completion_argument.py"
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", str(file_path), "E"],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_COMPLETION_ARGUMENT.PY_COMPLETE": "complete_zsh",
"_TYPER_COMPLETE_ARGS": "completion_argument.py E",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert "Emma" in result.stdout or "_files" in result.stdout
assert "ctx: completion_argument" in result.stderr
assert "arg is: name" in result.stderr
assert "incomplete is: E" in result.stderr


def test_completion_untyped_parameters():
file_path = Path(__file__).parent / "assets/completion_no_types.py"
result = subprocess.run(
Expand Down
1 change: 1 addition & 0 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,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,
autocompletion=get_param_completion(parameter_info.autocompletion),
# Rich settings
rich_help_panel=parameter_info.rich_help_panel,
Expand Down

0 comments on commit dcb45b1

Please sign in to comment.