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

12 statements  

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

1import typer 1abcdefghij

2 

3valid_completion_items = [ 1abcdefghij

4 ("Camila", "The reader of books."), 

5 ("Carlos", "The writer of scripts."), 

6 ("Sebastian", "The type hints guy."), 

7] 

8 

9 

10def complete_name(ctx: typer.Context, incomplete: str): 1abcdefghij

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

12 for name, help_text in valid_completion_items: 1abcdefghij

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

14 yield (name, help_text) 1abcdefghij

15 

16 

17app = typer.Typer() 1abcdefghij

18 

19 

20@app.command() 1abcdefghij

21def main( 1abcdefghij

22 name: list[str] = typer.Option( 

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

24 ), 

25): 

26 for n in name: 1abcdefghij

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

28 

29 

30if __name__ == "__main__": 1abcdefghij

31 app() 1abcdefghij