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

17 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-19 20:29 +0000

1import click 1abcdefghi

2import typer 1abcdefghi

3from typing_extensions import Annotated 1abcdefghi

4 

5 

6class CustomClass: 1abcdefghi

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

8 self.value = value 1abcdefghi

9 

10 def __repr__(self): 1abcdefghi

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

12 

13 

14class CustomClassParser(click.ParamType): 1abcdefghi

15 name = "CustomClass" 1abcdefghi

16 

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

18 return CustomClass(value * 3) 1abcdefghi

19 

20 

21app = typer.Typer() 1abcdefghi

22 

23 

24@app.command() 1abcdefghi

25def main( 1abcdefghi

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

27 custom_opt: Annotated[ 

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

29 ] = "Foo", 

30): 

31 print(f"custom_arg is {custom_arg}") 1abcdefghi

32 print(f"--custom-opt is {custom_opt}") 1abcdefghi

33 

34 

35if __name__ == "__main__": 1abcdefghi

36 app() 1abcdefghi