-
-
Notifications
You must be signed in to change notification settings - Fork 858
Expand file tree
/
Copy pathtest_type_conversion.py
More file actions
179 lines (135 loc) · 4.44 KB
/
test_type_conversion.py
File metadata and controls
179 lines (135 loc) · 4.44 KB
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
from enum import Enum
from pathlib import Path
from typing import Any
import click
import pytest
import typer
from typer.testing import CliRunner
runner = CliRunner()
def test_optional():
app = typer.Typer()
@app.command()
def opt(user: str | None = None):
if user:
print(f"User: {user}")
else:
print("No user")
result = runner.invoke(app)
assert result.exit_code == 0
assert "No user" in result.output
result = runner.invoke(app, ["--user", "Camila"])
assert result.exit_code == 0
assert "User: Camila" in result.output
def test_union_type_optional():
app = typer.Typer()
@app.command()
def opt(user: str | None = None):
if user:
print(f"User: {user}")
else:
print("No user")
result = runner.invoke(app)
assert result.exit_code == 0
assert "No user" in result.output
result = runner.invoke(app, ["--user", "Camila"])
assert result.exit_code == 0
assert "User: Camila" in result.output
def test_optional_tuple():
app = typer.Typer()
@app.command()
def opt(number: tuple[int, int] | None = None):
if number:
print(f"Number: {number}")
else:
print("No number")
result = runner.invoke(app)
assert result.exit_code == 0
assert "No number" in result.output
result = runner.invoke(app, ["--number", "4", "2"])
assert result.exit_code == 0
assert "Number: (4, 2)" in result.output
def test_no_type():
app = typer.Typer()
@app.command()
def no_type(user):
print(f"User: {user}")
result = runner.invoke(app, ["Camila"])
assert result.exit_code == 0
assert "User: Camila" in result.output
class SomeEnum(Enum):
ONE = "one"
TWO = "two"
THREE = "three"
def test_enum_convertor_already_converted():
"""If the value is already an enum member, the convertor should return it as-is."""
from typer.main import generate_enum_convertor
convertor = generate_enum_convertor(SomeEnum)
result = convertor(SomeEnum.ONE)
assert result is SomeEnum.ONE
@pytest.mark.parametrize(
"type_annotation",
[list[Path], list[SomeEnum], list[str]],
)
def test_list_parameters_convert_to_lists(type_annotation):
# Lists containing objects that are converted by Click (i.e. not Path or Enum)
# should not be inadvertently converted to tuples
expected_element_type = type_annotation.__args__[0]
app = typer.Typer()
@app.command()
def list_conversion(container: type_annotation):
assert isinstance(container, list)
for element in container:
assert isinstance(element, expected_element_type)
result = runner.invoke(app, ["one", "two", "three"])
assert result.exit_code == 0
@pytest.mark.parametrize(
"type_annotation",
[
tuple[str, str],
tuple[str, Path],
tuple[Path, Path],
tuple[str, SomeEnum],
tuple[SomeEnum, SomeEnum],
],
)
def test_tuple_parameter_elements_are_converted_recursively(type_annotation):
# Tuple elements that aren't converted by Click (i.e. Path or Enum)
# should be recursively converted by Typer
expected_element_types = type_annotation.__args__
app = typer.Typer()
@app.command()
def tuple_recursive_conversion(container: type_annotation):
assert isinstance(container, tuple)
for element, expected_type in zip(
container, expected_element_types, strict=True
):
assert isinstance(element, expected_type)
result = runner.invoke(app, ["one", "two"])
assert result.exit_code == 0
def test_custom_parse():
app = typer.Typer()
@app.command()
def custom_parser(
hex_value: int = typer.Argument(None, parser=lambda x: int(x, 0)),
):
assert hex_value == 0x56
result = runner.invoke(app, ["0x56"])
assert result.exit_code == 0
def test_custom_click_type():
class BaseNumberParamType(click.ParamType):
name = "base_integer"
def convert(
self,
value: Any,
param: click.Parameter | None,
ctx: click.Context | None,
) -> Any:
return int(value, 0)
app = typer.Typer()
@app.command()
def custom_click_type(
hex_value: int = typer.Argument(None, click_type=BaseNumberParamType()),
):
assert hex_value == 0x56
result = runner.invoke(app, ["0x56"])
assert result.exit_code == 0