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

15 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-26 21:46 +0000

1from typing import Annotated 1abcdefg

2 

3import typer 1abcdefg

4from rich.console import Console 1abcdefg

5 

6valid_completion_items = [ 1abcdefg

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) 1abcdefg

13 

14 

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

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

17 for name, help_text in valid_completion_items: 1abcdefg

18 if name.startswith(incomplete): 1abcdefg

19 yield (name, help_text) 1abcdefg

20 

21 

22app = typer.Typer() 1abcdefg

23 

24 

25@app.command() 1abcdefg

26def main( 1abcdefg

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: 1abcdefg

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

34 

35 

36if __name__ == "__main__": 1abcdefg

37 app() 1abcdefg