test_annotated.py
This file
Path
tests/test_annotated.py
100 lines · 2.6 KiB · Python
Last changed by
âž– Drop support for Python 3.8 (#1464)Sebastián RamÃrezbefore adoptionfbff83e
History of this fileTake it
The walk stopped after 200 commits: lines older than f6718b8 point at it.
✨ Enable command suggestions on typo by default (#1371)Savannah Ostrowskibefore adoptionf6718b8
lines 1–2why, one step back1import sys2from pathlib import Pathâž– Drop support for Python 3.8 (#1464)Sebastián RamÃrezbefore adoptionfbff83e
line 3why, one step back3from typing import Annotated✨ Enable command suggestions on typo by default (#1371)Savannah Ostrowskibefore adoptionf6718b8
lines 4–100why, one step back45import typer6from typer.testing import CliRunner78from .utils import needs_py310910runner = CliRunner()111213def test_annotated_argument_with_default():14 app = typer.Typer()1516 @app.command()17 def cmd(val: Annotated[int, typer.Argument()] = 0):18 print(f"hello {val}")1920 result = runner.invoke(app)21 assert result.exit_code == 0, result.output22 assert "hello 0" in result.output2324 result = runner.invoke(app, ["42"])25 assert result.exit_code == 0, result.output26 assert "hello 42" in result.output272829@needs_py31030def test_annotated_argument_in_string_type_with_default():31 app = typer.Typer()3233 @app.command()34 def cmd(val: "Annotated[int, typer.Argument()]" = 0):35 print(f"hello {val}")3637 result = runner.invoke(app)38 assert result.exit_code == 0, result.output39 assert "hello 0" in result.output4041 result = runner.invoke(app, ["42"])42 assert result.exit_code == 0, result.output43 assert "hello 42" in result.output444546def test_annotated_argument_with_default_factory():47 app = typer.Typer()4849 def make_string():50 return "I made it"5152 @app.command()53 def cmd(val: Annotated[str, typer.Argument(default_factory=make_string)]):54 print(val)5556 result = runner.invoke(app)57 assert result.exit_code == 0, result.output58 assert "I made it" in result.output5960 result = runner.invoke(app, ["overridden"])61 assert result.exit_code == 0, result.output62 assert "overridden" in result.output636465def test_annotated_option_with_argname_doesnt_mutate_multiple_calls():66 app = typer.Typer()6768 @app.command()69 def cmd(force: Annotated[bool, typer.Option("--force")] = False):70 if force:71 print("Forcing operation")72 else:73 print("Not forcing")7475 result = runner.invoke(app)76 assert result.exit_code == 0, result.output77 assert "Not forcing" in result.output7879 result = runner.invoke(app, ["--force"])80 assert result.exit_code == 0, result.output81 assert "Forcing operation" in result.output828384def test_annotated_custom_path():85 app = typer.Typer()8687 class CustomPath(Path):88 # Subclassing Path was not fully supported before 3.1289 # https://docs.python.org/3.12/whatsnew/3.12.html90 if sys.version_info < (3, 12):91 _flavour = type(Path())._flavour9293 @app.command()94 def custom_parser(95 my_path: Annotated[CustomPath, typer.Argument(parser=CustomPath)],96 ):97 assert isinstance(my_path, CustomPath)9899 result = runner.invoke(app, "/some/quirky/path/implementation")100 assert result.exit_code == 0