fastapi / typer

Typer, build great CLIs. Easy to code. Based on Python type hints.
https://typer.tiangolo.com/
MIT License
15.68k stars 667 forks source link

No autocompletion and syntax highlighting in callback for typer.Context but they work using click.Context #346

Open Minipada opened 2 years ago

Minipada commented 2 years ago

First Check

Commit to Help

Example Code

import click
import typer

def validate_name_typer(ctx: typer.Context, param: typer.CallbackParam, value: str) -> str:
    params = ctx.command.params # Autocompletion and syntax highlighting don't work
    return value

def validate_name_click(ctx: click.Context, param: typer.CallbackParam, value: str) -> str:
    params = ctx.command.params # Autocompletion and syntax highlighting work
    return value

def main(name: str= typer.Option(...,callback=validate_name_typer)):
    typer.echo(f"Hello {name}")

if __name__ == "__main__":
    typer.run(main)

Description

On VSCode, I don't have autocompletion for the context in a callback. I am not sure where it comes from but this definitely seems a typer issue since it is working with Click.

image image

I added both in the example.

EDIT: I am here trying to get other options from this callback.

Operating System

Linux

Operating System Details

$ cat /etc/os-release 

NAME="Manjaro Linux" ID=manjaro ID_LIKE=arch BUILD_ID=rolling PRETTY_NAME="Manjaro Linux" ANSI_COLOR="32;1;24;144;200" HOME_URL="https://manjaro.org/" DOCUMENTATION_URL="https://wiki.manjaro.org/" SUPPORT_URL="https://manjaro.org/" BUG_REPORT_URL="https://bugs.manjaro.org/" LOGO=manjarolinux

Typer Version

0.4.0

Python Version

3.9.9

Additional Context

No response

patricksurry commented 2 years ago

having a similar weird issue where autocompletion is working fine in bash/osx, but in pwsh/win11 some of my autocomplete works, but others fail in the same script.

For example using def hi(name: List[str] = typer.Argument(..., autocompletion=something)): ... works fine to suggest completions starting from empty name or partial string, but if I drop the List and do def hi(name: str = typer.Argument(..., autocompletion=something)): ... then I get autocomplete suggestions if I [tab][tab] from an empty argument, but as soon as I have one or more characters incomplete string then [tab][tab] gives nothing and apparently my callback something isn't even called.

Tried various combinations of typer/click version but can't find a clue yet.

patricksurry commented 2 years ago

btw in your example you're using autocompletion rather than callback for typer?