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

17 statements  

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

1from typing import Annotated 1abcdefgh

2 

3import click 1abcdefgh

4import typer 1abcdefgh

5 

6 

7class CustomClass: 1abcdefgh

8 def __init__(self, value: str): 1abcdefgh

9 self.value = value 1abcdefgh

10 

11 def __repr__(self): 1abcdefgh

12 return f"<CustomClass: value={self.value}>" 1abcdefgh

13 

14 

15class CustomClassParser(click.ParamType): 1abcdefgh

16 name = "CustomClass" 1abcdefgh

17 

18 def convert(self, value, param, ctx): 1abcdefgh

19 return CustomClass(value * 3) 1abcdefgh

20 

21 

22app = typer.Typer() 1abcdefgh

23 

24 

25@app.command() 1abcdefgh

26def main( 1abcdefgh

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

28 custom_opt: Annotated[ 

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

30 ] = "Foo", 

31): 

32 print(f"custom_arg is {custom_arg}") 1abcdefgh

33 print(f"--custom-opt is {custom_opt}") 1abcdefgh

34 

35 

36if __name__ == "__main__": 1abcdefgh

37 app() 1abcdefgh