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

16 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-19 20:29 +0000

1from typing import List 1abcdefghi

2 

3import typer 1abcdefghi

4from rich.console import Console 1abcdefghi

5 

6valid_completion_items = [ 1abcdefghi

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

13 

14 

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

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

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

18 for name, help_text in valid_completion_items: 1abcdefghi

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

20 yield (name, help_text) 1abcdefghi

21 

22 

23app = typer.Typer() 1abcdefghi

24 

25 

26@app.command() 1abcdefghi

27def main( 1abcdefghi

28 name: List[str] = typer.Option( 

29 ["World"], help="The name to say hi to.", autocompletion=complete_name 

30 ), 

31): 

32 for n in name: 1abcdefghi

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

34 

35 

36if __name__ == "__main__": 1abcdefghi

37 app() 1abcdefghi