typer-cli icon indicating copy to clipboard operation
typer-cli copied to clipboard

Input validation callbacks are triggered 2 times for string, not for booleans

Open gauravgs opened this issue 1 year ago • 0 comments

For the below code, I see the "str" validation callback runs twice, but for bool vars it runs only once. Additionally in the non-interactive CLI, i.e. passing all params with -- in a single command, all callbacks are executed only once as expected.


from typing import Optional

import typer
from typing_extensions import Annotated



def version_callback(value: bool):
    print(f"Awesome CLI Version: 0.1.0")
    return value

def new_callback(value: str):
    print("In it")
    return value


def main(
    name: Annotated[str, typer.Option(prompt="Enter 'name'", callback=new_callback)],
    version: Annotated[
        bool, typer.Option(prompt="Enter 'version'", callback=version_callback)
    ],
):
    print(f"Hello {name}")
    print(version)


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

Can someone help with this?

To reproduce this issue, run this code once as python main.py (you will see new_callback called twice) and also run it as python main.py --name hello --version (no extra runs)

gauravgs avatar Oct 23 '23 13:10 gauravgs