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

15 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

4from rich.console import Console 1abcdefgh

5 

6valid_completion_items = [ 1abcdefgh

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

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

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

10] 

11 

12err_console = Console(stderr=True) 1abcdefgh

13 

14 

15def complete_name(args: list[str], incomplete: str): 1abcdefgh

16 err_console.print(f"{args}") 1abcdefgh

17 for name, help_text in valid_completion_items: 1abcdefgh

18 if name.startswith(incomplete): 1abcdefgh

19 yield (name, help_text) 1abcdefgh

20 

21 

22app = typer.Typer() 1abcdefgh

23 

24 

25@app.command() 1abcdefgh

26def main( 1abcdefgh

27 name: Annotated[ 

28 list[str], 

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

30 ] = ["World"], 

31): 

32 for n in name: 1abcdefgh

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

34 

35 

36if __name__ == "__main__": 1abcdefgh

37 app() 1abcdefgh