Coverage for tests / test_annotated.py: 100%
71 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
1import sys 1abcdefghij
2from pathlib import Path 1abcdefghij
3from typing import Annotated 1abcdefghij
5import pytest 1abcdefghij
6import typer 1abcdefghij
7from typer.testing import CliRunner 1abcdefghij
9runner = CliRunner() 1abcdefghij
12def test_annotated_argument_with_default(): 1abcdefghij
13 app = typer.Typer() 1abcdefghij
15 @app.command() 1abcdefghij
16 def cmd(val: Annotated[int, typer.Argument()] = 0): 1abcdefghij
17 print(f"hello {val}") 1abcdefghij
19 result = runner.invoke(app) 1abcdefghij
20 assert result.exit_code == 0, result.output 1abcdefghij
21 assert "hello 0" in result.output 1abcdefghij
23 result = runner.invoke(app, ["42"]) 1abcdefghij
24 assert result.exit_code == 0, result.output 1abcdefghij
25 assert "hello 42" in result.output 1abcdefghij
28def test_annotated_argument_in_string_type_with_default(): 1abcdefghij
29 app = typer.Typer() 1abcdefghij
31 @app.command() 1abcdefghij
32 def cmd(val: "Annotated[int, typer.Argument()]" = 0): 1abcdefghij
33 print(f"hello {val}") 1abcdefghij
35 result = runner.invoke(app) 1abcdefghij
36 assert result.exit_code == 0, result.output 1abcdefghij
37 assert "hello 0" in result.output 1abcdefghij
39 result = runner.invoke(app, ["42"]) 1abcdefghij
40 assert result.exit_code == 0, result.output 1abcdefghij
41 assert "hello 42" in result.output 1abcdefghij
44def test_annotated_argument_with_default_factory(): 1abcdefghij
45 app = typer.Typer() 1abcdefghij
47 def make_string(): 1abcdefghij
48 return "I made it" 1abcdefghij
50 @app.command() 1abcdefghij
51 def cmd(val: Annotated[str, typer.Argument(default_factory=make_string)]): 1abcdefghij
52 print(val) 1abcdefghij
54 result = runner.invoke(app) 1abcdefghij
55 assert result.exit_code == 0, result.output 1abcdefghij
56 assert "I made it" in result.output 1abcdefghij
58 result = runner.invoke(app, ["overridden"]) 1abcdefghij
59 assert result.exit_code == 0, result.output 1abcdefghij
60 assert "overridden" in result.output 1abcdefghij
63def test_annotated_option_with_argname_doesnt_mutate_multiple_calls(): 1abcdefghij
64 app = typer.Typer() 1abcdefghij
66 @app.command() 1abcdefghij
67 def cmd(force: Annotated[bool, typer.Option("--force")] = False): 1abcdefghij
68 if force: 1abcdefghij
69 print("Forcing operation") 1abcdefghij
70 else:
71 print("Not forcing") 1abcdefghij
73 result = runner.invoke(app) 1abcdefghij
74 assert result.exit_code == 0, result.output 1abcdefghij
75 assert "Not forcing" in result.output 1abcdefghij
77 result = runner.invoke(app, ["--force"]) 1abcdefghij
78 assert result.exit_code == 0, result.output 1abcdefghij
79 assert "Forcing operation" in result.output 1abcdefghij
82def test_annotated_custom_path(): 1abcdefghij
83 app = typer.Typer() 1abcdefghij
85 class CustomPath(Path): 1abcdefghij
86 # Subclassing Path was not fully supported before 3.12
87 # https://docs.python.org/3.12/whatsnew/3.12.html
88 if sys.version_info < (3, 12): 1abcdefghij
89 _flavour = type(Path())._flavour 1a
91 @app.command() 1abcdefghij
92 def custom_parser( 1abcdefghij
93 my_path: Annotated[CustomPath, typer.Argument(parser=CustomPath)],
94 ):
95 assert isinstance(my_path, CustomPath) 1abcdefghij
97 result = runner.invoke(app, "/some/quirky/path/implementation") 1abcdefghij
98 assert result.exit_code == 0 1abcdefghij
101def test_annotated_option_invalid(): 1abcdefghij
102 app = typer.Typer() 1abcdefghij
104 @app.command() 1abcdefghij
105 def cmd(value: Annotated[str, typer.Option(..., "foo-bar")]): 1abcdefghij
106 print(value) # pragma: no cover
108 with pytest.raises(ValueError, match="Invalid start character for option"): 1abcdefghij
109 runner.invoke(app, ["--help"], catch_exceptions=False) 1abcdefghij