typer icon indicating copy to clipboard operation
typer copied to clipboard

[BUG] Completions that include colons get separated in zsh

Open jaywonchung opened this issue 5 years ago • 3 comments

Describe the bug

I have completions (via the autocompletion keyword argument in Arguments and Options) that include a colon (:) in the string. In zsh, these completions get split into completion candidates and help texts at the colon.

To Reproduce

  • Create a file main.py with:
import typer

images = ["alpine:latest", "nvidia/cuda:10.0-devel-ubuntu18.04"]

def _complete(value: str) -> str:
    for image in images:
        if image.startswith(value):
            yield value

app = typer.Typer()

@app.command("image")
def hello(name: str = typer.Option(..., autocompletion=_complete)):
    typer.echo(name)

if __name__ == "__main__":
    app()
  • Trigger autocompletion with [TAB] on ZSH
$ <app_entrypoint> image --name [TAB]
  • It outputs:
$ <app_entrypoint> image --name [TAB]
alpine       -- latest
nvidia/cuda  -- 10.0-devel-ubuntu18.04

When I cycle through the completion candidates, only alpine and nvidia/cuda gets filled in.

  • But I expected it to output:
$ <app_entrypoint> image --name [TAB]
alpine:latest
nvidia/cuda:10.0-devel-ubuntu18.04

Environment

  • OS: Ubuntu 18.04
  • Typer Version: 0.3.2
  • Python version: 3.7.9
  • Zsh version: 5.4.2 (x86_64-ubuntu-linux-gnu)

Additional context

I tried the exact same thing in Bash, and it worked as expected (the output in "But I expected it to output").

jaywonchung avatar Sep 06 '20 14:09 jaywonchung

I don't think this is a bug in typer, I think this is just the nature / default behaviour of the zsh completion system. zsh expects the results of your completetion function to take the form completion_candidate:help_text and formats that output accordingly. There are ways of getting around that in zsh, like so: https://unix.stackexchange.com/questions/445889/use-colon-as-filename-separator-in-zsh-tab-completion

but I suspect this is beyond the scope of typer's zsh auto-completion support (I'm not a core contributor, i can't speak for them, this is just my two cents)

alextremblay avatar Dec 04 '20 15:12 alextremblay

@alextremblay I tried the snippet in the link but it didn't work. I want my autocompletion function to return something like

B6:AA:33:B5:C0:93 -- Lmi 10.0.201.222 0

but got B6 -- AA:33:B5:C0:93 -- Lmi 10.0.201.222 0 instead.

I have no idea how zsh completion works, can you help me configure zsh so that the completions work? Thank you!

tddschn avatar May 15 '22 13:05 tddschn

@tddschn sadly, no. i think this is something that would need to change in typer's auto-generated zsh autocomplete script.

you could try editing the generated script yourself, but then if you change your CLI's code and need to regenerate autocompletions, your changes will be wiped out

alextremblay avatar May 17 '22 14:05 alextremblay