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
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-09 12:36 +0000
1from typing import Annotated 1abcdefgh
3import click 1abcdefgh
4import typer 1abcdefgh
7class CustomClass: 1abcdefgh
8 def __init__(self, value: str): 1abcdefgh
9 self.value = value 1abcdefgh
11 def __repr__(self): 1abcdefgh
12 return f"<CustomClass: value={self.value}>" 1abcdefgh
15class CustomClassParser(click.ParamType): 1abcdefgh
16 name = "CustomClass" 1abcdefgh
18 def convert(self, value, param, ctx): 1abcdefgh
19 return CustomClass(value * 3) 1abcdefgh
22app = typer.Typer() 1abcdefgh
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
36if __name__ == "__main__": 1abcdefgh
37 app() 1abcdefgh