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

16 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(ctx: typer.Context, args: list[str], incomplete: str): 1abcdefg

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

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

18 for name, help_text in valid_completion_items: 1abcdefg

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

20 yield (name, help_text) 1abcdefg

21 

22 

23app = typer.Typer() 1abcdefg

24 

25 

26@app.command() 1abcdefg

27def main( 1abcdefg

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

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

35 

36 

37if __name__ == "__main__": 1abcdefg

38 app() 1abcdefg