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

16 statements  

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

1from typing import Annotated 1abcdefghij

2 

3import typer 1abcdefghij

4from rich.console import Console 1abcdefghij

5 

6valid_completion_items = [ 1abcdefghij

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

13 

14 

15def complete_name(ctx: typer.Context, args: list[str], incomplete: str): 1abcdefghij

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

17 names = ctx.params.get("name") or [] 1abcdefghij

18 for name, help_text in valid_completion_items: 1abcdefghij

19 if name.startswith(incomplete) and name not in names: 1abcdefghij

20 yield (name, help_text) 1abcdefghij

21 

22 

23app = typer.Typer() 1abcdefghij

24 

25 

26@app.command() 1abcdefghij

27def main( 1abcdefghij

28 name: Annotated[ 

29 list[str], 

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

31 ] = ["World"], 

32): 

33 for n in name: 1abcdefghij

34 print(f"Hello {n}") 1abcdefghij

35 

36 

37if __name__ == "__main__": 1abcdefghij

38 app() 1abcdefghij