Coverage for docs_src / options_autocompletion / tutorial007_an_py39.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-02-09 12:36 +0000

1from typing import Annotated 1abcdefgh

2 

3import typer 1abcdefgh

4 

5valid_completion_items = [ 1abcdefgh

6 ("Camila", "The reader of books."), 

7 ("Carlos", "The writer of scripts."), 

8 ("Sebastian", "The type hints guy."), 

9] 

10 

11 

12def complete_name(ctx: typer.Context, incomplete: str): 1abcdefgh

13 names = ctx.params.get("name") or [] 1abcdefgh

14 for name, help_text in valid_completion_items: 1abcdefgh

15 if name.startswith(incomplete) and name not in names: 1abcdefgh

16 yield (name, help_text) 1abcdefgh

17 

18 

19app = typer.Typer() 1abcdefgh

20 

21 

22@app.command() 1abcdefgh

23def main( 1abcdefgh

24 name: Annotated[ 

25 list[str], 

26 typer.Option(help="The name to say hi to.", autocompletion=complete_name), 

27 ] = ["World"], 

28): 

29 for n in name: 1abcdefgh

30 print(f"Hello {n}") 1abcdefgh

31 

32 

33if __name__ == "__main__": 1abcdefgh

34 app() 1abcdefgh