Skip to content
Myr

test_annotated.py

This file

Path

tests/test_annotated.py

97 lines · 2.6 KiB · Python

Last changed by

➖ Drop support for Python 3.9 (#1546)Sebastián Ramírezbefore adoption28035df

History of this file

Take it

The walk stopped after 200 commits: lines older than 47262dc point at it.

📝 Update release notesgithub-actions[bot]before adoption47262dc

lines 1–97why, one step back
1import sys
2from pathlib import Path3from typing import Annotated45import typer6from typer.testing import CliRunner78runner = CliRunner()91011def test_annotated_argument_with_default():12 app = typer.Typer()1314 @app.command()15 def cmd(val: Annotated[int, typer.Argument()] = 0):16 print(f"hello {val}")1718 result = runner.invoke(app)19 assert result.exit_code == 0, result.output20 assert "hello 0" in result.output2122 result = runner.invoke(app, ["42"])23 assert result.exit_code == 0, result.output24 assert "hello 42" in result.output252627def test_annotated_argument_in_string_type_with_default():28 app = typer.Typer()2930 @app.command()31 def cmd(val: "Annotated[int, typer.Argument()]" = 0):32 print(f"hello {val}")3334 result = runner.invoke(app)35 assert result.exit_code == 0, result.output36 assert "hello 0" in result.output3738 result = runner.invoke(app, ["42"])39 assert result.exit_code == 0, result.output40 assert "hello 42" in result.output414243def test_annotated_argument_with_default_factory():44 app = typer.Typer()4546 def make_string():47 return "I made it"4849 @app.command()50 def cmd(val: Annotated[str, typer.Argument(default_factory=make_string)]):51 print(val)5253 result = runner.invoke(app)54 assert result.exit_code == 0, result.output55 assert "I made it" in result.output5657 result = runner.invoke(app, ["overridden"])58 assert result.exit_code == 0, result.output59 assert "overridden" in result.output606162def test_annotated_option_with_argname_doesnt_mutate_multiple_calls():63 app = typer.Typer()6465 @app.command()66 def cmd(force: Annotated[bool, typer.Option("--force")] = False):67 if force:68 print("Forcing operation")69 else:70 print("Not forcing")7172 result = runner.invoke(app)73 assert result.exit_code == 0, result.output74 assert "Not forcing" in result.output7576 result = runner.invoke(app, ["--force"])77 assert result.exit_code == 0, result.output78 assert "Forcing operation" in result.output798081def test_annotated_custom_path():82 app = typer.Typer()8384 class CustomPath(Path):85 # Subclassing Path was not fully supported before 3.1286 # https://docs.python.org/3.12/whatsnew/3.12.html87 if sys.version_info < (3, 12):88 _flavour = type(Path())._flavour8990 @app.command()91 def custom_parser(92 my_path: Annotated[CustomPath, typer.Argument(parser=CustomPath)],93 ):94 assert isinstance(my_path, CustomPath)9596 result = runner.invoke(app, "/some/quirky/path/implementation")97 assert result.exit_code == 0