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

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 18:26 +0000

1import click 1habcdefg

2import typer 1habcdefg

3from typing_extensions import Annotated 1habcdefg

4 

5 

6class CustomClass: 1habcdefg

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

8 self.value = value 1habcdefg

9 

10 def __repr__(self): 1habcdefg

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

12 

13 

14class CustomClassParser(click.ParamType): 1habcdefg

15 name = "CustomClass" 1habcdefg

16 

17 def convert(self, value, param, ctx): 1habcdefg

18 return CustomClass(value * 3) 1habcdefg

19 

20 

21def main( 1abcdefg

22 custom_arg: Annotated[CustomClass, typer.Argument(click_type=CustomClassParser())], 

23 custom_opt: Annotated[ 

24 CustomClass, typer.Option(click_type=CustomClassParser()) 

25 ] = "Foo", 

26): 

27 print(f"custom_arg is {custom_arg}") 1habcdefg

28 print(f"--custom-opt is {custom_opt}") 1habcdefg

29 

30 

31if __name__ == "__main__": 1habcdefg

32 typer.run(main) 1habcdefg