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

14 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

4 

5valid_completion_items = [ 1abcdefg

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

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

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

9] 

10 

11 

12def complete_name(incomplete: str): 1abcdefg

13 completion = [] 1abcdefg

14 for name, help_text in valid_completion_items: 1abcdefg

15 if name.startswith(incomplete): 1abcdefg

16 completion_item = (name, help_text) 1abcdefg

17 completion.append(completion_item) 1abcdefg

18 return completion 1abcdefg

19 

20 

21app = typer.Typer() 1abcdefg

22 

23 

24@app.command() 1abcdefg

25def main( 1abcdefg

26 name: Annotated[ 

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

28 ] = "World", 

29): 

30 print(f"Hello {name}") 1abcdefg

31 

32 

33if __name__ == "__main__": 1abcdefg

34 app() 1abcdefg