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

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-14 00:18 +0000

1import click 1iabcdefgh

2import typer 1iabcdefgh

3from typing_extensions import Annotated 1iabcdefgh

4 

5 

6class CustomClass: 1iabcdefgh

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

8 self.value = value 1iabcdefgh

9 

10 def __repr__(self): 1iabcdefgh

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

12 

13 

14class CustomClassParser(click.ParamType): 1iabcdefgh

15 name = "CustomClass" 1iabcdefgh

16 

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

18 return CustomClass(value * 3) 1iabcdefgh

19 

20 

21def main( 1abcdefgh

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}") 1iabcdefgh

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

29 

30 

31if __name__ == "__main__": 1iabcdefgh

32 typer.run(main) 1iabcdefgh