Coverage for docs_src / parameter_types / custom_types / tutorial001.py: 100%

14 statements  

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

1from typing import Annotated 1abcdefghij

2 

3import typer 1abcdefghij

4 

5 

6class CustomClass: 1abcdefghij

7 def __init__(self, value: str): 1abcdefghij

8 self.value = value 1abcdefghij

9 

10 def __str__(self): 1abcdefghij

11 return f"<CustomClass: value={self.value}>" 1abcdefghij

12 

13 

14def parse_custom_class(value: str): 1abcdefghij

15 return CustomClass(value * 2) 1abcdefghij

16 

17 

18app = typer.Typer() 1abcdefghij

19 

20 

21@app.command() 1abcdefghij

22def main( 1abcdefghij

23 custom_arg: Annotated[CustomClass, typer.Argument(parser=parse_custom_class)], 

24 custom_opt: Annotated[CustomClass, typer.Option(parser=parse_custom_class)] = "Foo", 

25): 

26 print(f"custom_arg is {custom_arg}") 1abcdefghij

27 print(f"--custom-opt is {custom_opt}") 1abcdefghij

28 

29 

30if __name__ == "__main__": 1abcdefghij

31 app() 1abcdefghij