1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
import pytest
import typer
from typer.testing import CliRunner
from typer.utils import (
AnnotatedParamWithDefaultValueError,
DefaultFactoryAndDefaultValueError,
MixedAnnotatedAndDefaultStyleError,
MultipleTyperAnnotationsError,
_split_annotation_from_typer_annotations,
)
from typing_extensions import Annotated
runner = CliRunner()
def test_split_annotations_from_typer_annotations_simple():
# Simple sanity check that this utility works. If this isn't working on a given
# python version, then no other tests for Annotated will work.
given = Annotated[str, typer.Argument()]
base, typer_annotations = _split_annotation_from_typer_annotations(given)
assert base is str
# No equality check on the param types. Checking the length is sufficient.
assert len(typer_annotations) == 1
def test_forbid_default_value_in_annotated_argument():
app = typer.Typer()
# This test case only works with `typer.Argument`. `typer.Option` uses positionals
# for param_decls too.
@app.command()
def cmd(my_param: Annotated[str, typer.Argument("foo")]): ... # pragma: no cover
with pytest.raises(AnnotatedParamWithDefaultValueError) as excinfo:
runner.invoke(app)
assert vars(excinfo.value) == {
"param_type": typer.models.ArgumentInfo,
"argument_name": "my_param",
}
def test_allow_options_to_have_names():
app = typer.Typer()
@app.command()
def cmd(my_param: Annotated[str, typer.Option("--some-opt")]):
print(my_param)
result = runner.invoke(app, ["--some-opt", "hello"])
assert result.exit_code == 0, result.output
assert "hello" in result.output
@pytest.mark.parametrize(
["param", "param_info_type"],
[
(typer.Argument, typer.models.ArgumentInfo),
(typer.Option, typer.models.OptionInfo),
],
)
def test_forbid_annotated_param_and_default_param(param, param_info_type):
app = typer.Typer()
@app.command()
def cmd(my_param: Annotated[str, param()] = param("foo")): ... # pragma: no cover
with pytest.raises(MixedAnnotatedAndDefaultStyleError) as excinfo:
runner.invoke(app)
assert vars(excinfo.value) == {
"argument_name": "my_param",
"annotated_param_type": param_info_type,
"default_param_type": param_info_type,
}
def test_forbid_multiple_typer_params_in_annotated():
app = typer.Typer()
@app.command()
def cmd(
my_param: Annotated[str, typer.Argument(), typer.Argument()],
): ... # pragma: no cover
with pytest.raises(MultipleTyperAnnotationsError) as excinfo:
runner.invoke(app)
assert vars(excinfo.value) == {"argument_name": "my_param"}
def test_allow_multiple_non_typer_params_in_annotated():
app = typer.Typer()
@app.command()
def cmd(my_param: Annotated[str, "someval", typer.Argument(), 4] = "hello"):
print(my_param)
result = runner.invoke(app)
# Should behave like normal
assert result.exit_code == 0, result.output
assert "hello" in result.output
@pytest.mark.parametrize(
["param", "param_info_type"],
[
(typer.Argument, typer.models.ArgumentInfo),
(typer.Option, typer.models.OptionInfo),
],
)
def test_forbid_default_factory_and_default_value_in_annotated(param, param_info_type):
def make_string():
return "foo" # pragma: no cover
app = typer.Typer()
@app.command()
def cmd(
my_param: Annotated[str, param(default_factory=make_string)] = "hello",
): ... # pragma: no cover
with pytest.raises(DefaultFactoryAndDefaultValueError) as excinfo:
runner.invoke(app)
assert vars(excinfo.value) == {
"argument_name": "my_param",
"param_type": param_info_type,
}
@pytest.mark.parametrize(
"param",
[
typer.Argument,
typer.Option,
],
)
def test_allow_default_factory_with_default_param(param):
def make_string():
return "foo"
app = typer.Typer()
@app.command()
def cmd(my_param: str = param(default_factory=make_string)):
print(my_param)
result = runner.invoke(app)
assert result.exit_code == 0, result.output
assert "foo" in result.output
@pytest.mark.parametrize(
["param", "param_info_type"],
[
(typer.Argument, typer.models.ArgumentInfo),
(typer.Option, typer.models.OptionInfo),
],
)
def test_forbid_default_and_default_factory_with_default_param(param, param_info_type):
def make_string():
return "foo" # pragma: no cover
app = typer.Typer()
@app.command()
def cmd(
my_param: str = param("hi", default_factory=make_string),
): ... # pragma: no cover
with pytest.raises(DefaultFactoryAndDefaultValueError) as excinfo:
runner.invoke(app)
assert vars(excinfo.value) == {
"argument_name": "my_param",
"param_type": param_info_type,
}
@pytest.mark.parametrize(
["error", "message"],
[
(
AnnotatedParamWithDefaultValueError(
argument_name="my_argument",
param_type=typer.models.ArgumentInfo,
),
"`Argument` default value cannot be set in `Annotated` for 'my_argument'. Set the default value with `=` instead.",
),
(
MixedAnnotatedAndDefaultStyleError(
argument_name="my_argument",
annotated_param_type=typer.models.OptionInfo,
default_param_type=typer.models.ArgumentInfo,
),
"Cannot specify `Option` in `Annotated` and `Argument` as a default value together for 'my_argument'",
),
(
MixedAnnotatedAndDefaultStyleError(
argument_name="my_argument",
annotated_param_type=typer.models.OptionInfo,
default_param_type=typer.models.OptionInfo,
),
"Cannot specify `Option` in `Annotated` and default value together for 'my_argument'",
),
(
MixedAnnotatedAndDefaultStyleError(
argument_name="my_argument",
annotated_param_type=typer.models.ArgumentInfo,
default_param_type=typer.models.ArgumentInfo,
),
"Cannot specify `Argument` in `Annotated` and default value together for 'my_argument'",
),
(
MultipleTyperAnnotationsError(
argument_name="my_argument",
),
"Cannot specify multiple `Annotated` Typer arguments for 'my_argument'",
),
(
DefaultFactoryAndDefaultValueError(
argument_name="my_argument",
param_type=typer.models.OptionInfo,
),
"Cannot specify `default_factory` and a default value together for `Option`",
),
],
)
def test_error_rendering(error, message):
assert str(error) == message
|