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

14 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 typing_extensions import Annotated 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 

12 

13def complete_name(ctx: typer.Context, incomplete: str): 1abcdefghi

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

15 for name, help_text in valid_completion_items: 1abcdefghi

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

17 yield (name, help_text) 1abcdefghi

18 

19 

20app = typer.Typer() 1abcdefghi

21 

22 

23@app.command() 1abcdefghi

24def main( 1abcdefghi

25 name: Annotated[ 

26 List[str], 

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

28 ] = ["World"], 

29): 

30 for n in name: 1abcdefghi

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

32 

33 

34if __name__ == "__main__": 1abcdefghi

35 app() 1abcdefghi