Skip to content
Myr

📝 Update docs examples for custom param types using `Annotated`, fix overloads for `typer.Argument` (#594)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 📝 Add source examples for custom parameter types with Annotated * ✅ Add tests for custom parameters with Annotated * 📝 Update docs for custom parameters with Annotated * ♻️ Fix overloads default in Argument after Annotated * ✅ Fix test for custom param types

authored by Sebastián Ramírez · committed by GitHub · parent 8c59b68 · Browse files · 9 files+169 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+169 added−12 removed

  • tests:+79 added−1 removed
  • other:+90 added−11 removed

9 files+169 added−12 removed

modified:docs/tutorial/parameter-types/custom-types.md+27 added-6 removed
added:docs_src/parameter_types/custom_types/tutorial001_an.py+26 added
1import typer2from typing_extensions import Annotated345class CustomClass:6 def __init__(self, value: str):7 self.value = value89 def __str__(self):10 return f"<CustomClass: value={self.value}>"111213def parse_custom_class(value: str):14 return CustomClass(value * 2)151617def main(18 custom_arg: Annotated[CustomClass, typer.Argument(parser=parse_custom_class)],19 custom_opt: Annotated[CustomClass, typer.Option(parser=parse_custom_class)] = "Foo",20):21 print(f"custom_arg is {custom_arg}")22 print(f"--custom-opt is {custom_opt}")232425if __name__ == "__main__":26 typer.run(main)
modified:docs_src/parameter_types/custom_types/tutorial001.py+1 added-1 removed
12 unmodified lines
13 return CustomClass(value * 2)141516def main(17 custom_arg: CustomClass = typer.Argument("X", parser=parse_custom_class),17 custom_arg: CustomClass = typer.Argument(parser=parse_custom_class),18 custom_opt: CustomClass = typer.Option("Y", parser=parse_custom_class),19):20 print(f"custom_arg is {custom_arg}")21 print(f"--custom-opt is {custom_opt}")
4 unmodified lines
added:docs_src/parameter_types/custom_types/tutorial002_an.py+32 added
1import click2import typer3from typing_extensions import Annotated456class CustomClass:7 def __init__(self, value: str):8 self.value = value910 def __repr__(self):11 return f"<CustomClass: value={self.value}>"121314class CustomClassParser(click.ParamType):15 name = "CustomClass"1617 def convert(self, value, param, ctx):18 return CustomClass(value * 3)192021def main(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}")28 print(f"--custom-opt is {custom_opt}")293031if __name__ == "__main__":32 typer.run(main)
modified:docs_src/parameter_types/custom_types/tutorial002.py+2 added-2 removed
16 unmodified lines
17 return CustomClass(value * 3)181920def main(21 custom_arg: CustomClass = typer.Argument("X", click_type=CustomClassParser()),22 custom_opt: CustomClass = typer.Option("Y", click_type=CustomClassParser()),21 custom_arg: CustomClass = typer.Argument(click_type=CustomClassParser()),22 custom_opt: CustomClass = typer.Option("Foo", click_type=CustomClassParser()),23):24 print(f"custom_arg is {custom_arg}")25 print(f"--custom-opt is {custom_opt}")26
3 unmodified lines
added:tests/test_tutorial/test_parameter_types/test_custom_types/test_tutorial001_an.py+39 added
1import subprocess2import sys34import typer5from typer.testing import CliRunner67from docs_src.parameter_types.custom_types import tutorial001_an as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)131415def test_help():16 result = runner.invoke(app, ["--help"])17 assert result.exit_code == 0181920def test_parse_custom_type():21 result = runner.invoke(app, ["0", "--custom-opt", "1"])22 assert "custom_arg is <CustomClass: value=00>" in result.output23 assert "custom-opt is <CustomClass: value=11>" in result.output242526def test_parse_custom_type_with_default():27 result = runner.invoke(app, ["0"])28 assert "custom_arg is <CustomClass: value=00>" in result.output29 assert "custom-opt is <CustomClass: value=FooFoo>" in result.output303132def test_script():33 result = subprocess.run(34 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],35 stdout=subprocess.PIPE,36 stderr=subprocess.PIPE,37 encoding="utf-8",38 )39 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_custom_types/test_tutorial002_an.py+39 added
1import subprocess2import sys34import typer5from typer.testing import CliRunner67from docs_src.parameter_types.custom_types import tutorial002_an as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)131415def test_help():16 result = runner.invoke(app, ["--help"])17 assert result.exit_code == 0181920def test_parse_custom_type():21 result = runner.invoke(app, ["0", "--custom-opt", "1"])22 assert "custom_arg is <CustomClass: value=000>" in result.output23 assert "custom-opt is <CustomClass: value=111>" in result.output242526def test_parse_custom_type_with_default():27 result = runner.invoke(app, ["0"])28 assert "custom_arg is <CustomClass: value=000>" in result.output29 assert "custom-opt is <CustomClass: value=FooFooFoo>" in result.output303132def test_script():33 result = subprocess.run(34 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],35 stdout=subprocess.PIPE,36 stderr=subprocess.PIPE,37 encoding="utf-8",38 )39 assert "Usage" in result.stdout
modified:tests/test_tutorial/test_parameter_types/test_custom_types/test_tutorial002.py+1 added-1 removed
24 unmodified lines
2526def test_parse_custom_type_with_default():27 result = runner.invoke(app, ["0"])28 assert "custom_arg is <CustomClass: value=000>" in result.output29 assert "custom-opt is <CustomClass: value=YYY>" in result.output29 assert "custom-opt is <CustomClass: value=FooFooFoo>" in result.output303132def test_script():33 result = subprocess.run(
6 unmodified lines
modified:typer/params.py+2 added-2 removed
252 unmodified lines
253254@overload255def Argument(256 # Parameter257 default: Optional[Any],257 default: Optional[Any] = ...,258 *,259 callback: Optional[Callable[..., Any]] = None,260 metavar: Optional[str] = None,261 expose_value: bool = True,
45 unmodified lines
307308@overload309def Argument(310 # Parameter311 default: Optional[Any],311 default: Optional[Any] = ...,312 *,313 callback: Optional[Callable[..., Any]] = None,314 metavar: Optional[str] = None,315 expose_value: bool = True,
147 unmodified lines