test_annotated.py
This file
Path
tests/test_annotated.py
109 lines · 2.9 KiB · Python
Last changed by
➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
History of this fileTake it
The walk stopped after 200 commits: lines older than 94484c4 point at it.
📝 Update release notesgithub-actions[bot]before adoption94484c4
lines 1–4why, one step back1import sys2from pathlib import Path3from typing import Annotated4➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
line 5why, one step back5import pytest📝 Update release notesgithub-actions[bot]before adoption94484c4
lines 6–98why, one step back6import typer7from typer.testing import CliRunner89runner = CliRunner()101112def test_annotated_argument_with_default():13 app = typer.Typer()1415 @app.command()16 def cmd(val: Annotated[int, typer.Argument()] = 0):17 print(f"hello {val}")1819 result = runner.invoke(app)20 assert result.exit_code == 0, result.output21 assert "hello 0" in result.output2223 result = runner.invoke(app, ["42"])24 assert result.exit_code == 0, result.output25 assert "hello 42" in result.output262728def test_annotated_argument_in_string_type_with_default():29 app = typer.Typer()3031 @app.command()32 def cmd(val: "Annotated[int, typer.Argument()]" = 0):33 print(f"hello {val}")3435 result = runner.invoke(app)36 assert result.exit_code == 0, result.output37 assert "hello 0" in result.output3839 result = runner.invoke(app, ["42"])40 assert result.exit_code == 0, result.output41 assert "hello 42" in result.output424344def test_annotated_argument_with_default_factory():45 app = typer.Typer()4647 def make_string():48 return "I made it"4950 @app.command()51 def cmd(val: Annotated[str, typer.Argument(default_factory=make_string)]):52 print(val)5354 result = runner.invoke(app)55 assert result.exit_code == 0, result.output56 assert "I made it" in result.output5758 result = runner.invoke(app, ["overridden"])59 assert result.exit_code == 0, result.output60 assert "overridden" in result.output616263def test_annotated_option_with_argname_doesnt_mutate_multiple_calls():64 app = typer.Typer()6566 @app.command()67 def cmd(force: Annotated[bool, typer.Option("--force")] = False):68 if force:69 print("Forcing operation")70 else:71 print("Not forcing")7273 result = runner.invoke(app)74 assert result.exit_code == 0, result.output75 assert "Not forcing" in result.output7677 result = runner.invoke(app, ["--force"])78 assert result.exit_code == 0, result.output79 assert "Forcing operation" in result.output808182def test_annotated_custom_path():83 app = typer.Typer()8485 class CustomPath(Path):86 # Subclassing Path was not fully supported before 3.1287 # https://docs.python.org/3.12/whatsnew/3.12.html88 if sys.version_info < (3, 12):89 _flavour = type(Path())._flavour9091 @app.command()92 def custom_parser(93 my_path: Annotated[CustomPath, typer.Argument(parser=CustomPath)],94 ):95 assert isinstance(my_path, CustomPath)9697 result = runner.invoke(app, "/some/quirky/path/implementation")98 assert result.exit_code == 0➖ Vendor Click and streamline Typer's functionality and code base (#1774)Sofie Van Landeghembefore adoption1829d73
lines 99–109why, one step back99100101def test_annotated_option_invalid():102 app = typer.Typer()103104 @app.command()105 def cmd(value: Annotated[str, typer.Option(..., "foo-bar")]):106 print(value) # pragma: no cover107108 with pytest.raises(ValueError, match="Invalid start character for option"):109 runner.invoke(app, ["--help"], catch_exceptions=False)