Debian Codegraph @

tests/test_bind_generic_class.py

raw symbols SCIP
1 from textwrap import dedent
2 from typing import Annotated, Literal
3
4 import pytest
5
6 from cyclopts import Parameter
7 from cyclopts.exceptions import MissingArgumentError
8
9
10 class Outfit:
11 def __init__(self, body: str, head: str):
12 self.body = body
13 self.head = head
14
15 def __eq__(self, other):
16 if not isinstance(other, type(self)):
17 return False
18 return self.body == other.body and self.head == other.head
19
20
21 class User:
22 def __init__(
23 self,
24 id: int,
25 name: str = "John Doe",
26 tastes: dict[str, int] | None = None,
27 outfit: Annotated[Outfit, Parameter(accepts_keys=True)] | None = None,
28 ):
29 self.id = id
30 self.name = name
31 self.tastes = tastes if tastes is not None else {}
32 self.outfit = outfit
33
34 def __eq__(self, other):
35 if not isinstance(other, type(self)):
36 return False
37 return (
38 self.id == other.id
39 and self.name == other.name
40 and self.tastes == other.tastes
41 and self.outfit == other.outfit
42 )
43
44
45 def test_bind_generic_class_accepts_keys_true(app, assert_parse_args):
46 @app.command
47 def foo(user: Annotated[User, Parameter(accepts_keys=True)]):
48 pass
49
50 assert_parse_args(
51 foo,
52 "foo --user.id=123 --user.tastes.wine=9 --user.tastes.cheese=7 --user.tastes.cabbage=1 --user.outfit.body=t-shirt --user.outfit.head=baseball-cap",
53 User(id=123, tastes={"wine": 9, "cheese": 7, "cabbage": 1}, outfit=Outfit(body="t-shirt", head="baseball-cap")),
54 )
55
56
57 def test_bind_generic_class_accepts_keys_none_1_args(app, assert_parse_args, console):
58 class User:
59 def __init__(self, age: int):
60 self.age = age
61
62 def __eq__(self, other):
63 if not isinstance(other, type(self)):
64 return False
65 return self.age == other.age
66
67 @app.command
68 def foo(user: User):
69 pass
70
71 assert_parse_args(foo, "foo 100", User(100))
72
73 with console.capture() as capture:
74 app("foo --help", console=console)
75
76 actual = capture.get()
77
78 expected = dedent(
79 """\
80 Usage: test_bind_generic_class foo USER.AGE
81
82 ╭─ Parameters ───────────────────────────────────────────────────────╮
83 │ * USER.AGE --user.age [required] │
84 ╰────────────────────────────────────────────────────────────────────╯
85 """
86 )
87
88 assert actual == expected
89
90
91 def test_bind_generic_class_accepts_keys_false_1_args(app, assert_parse_args, console):
92 class User:
93 def __init__(self, age: int):
94 self.age = age
95
96 def __eq__(self, other):
97 if not isinstance(other, type(self)):
98 return False
99 return self.age == other.age
100
101 @app.command
102 def foo(user: Annotated[User, Parameter(accepts_keys=False)]):
103 pass
104
105 assert_parse_args(foo, "foo 100", User(100))
106
107 with console.capture() as capture:
108 app("foo --help", console=console)
109
110 actual = capture.get()
111
112 expected = dedent(
113 """\
114 Usage: test_bind_generic_class foo USER
115
116 ╭─ Parameters ───────────────────────────────────────────────────────╮
117 │ * USER --user [required] │
118 ╰────────────────────────────────────────────────────────────────────╯
119 """
120 )
121
122 assert actual == expected
123
124
125 class Coordinates:
126 def __init__(
127 self,
128 x: float,
129 y: float,
130 *,
131 color: Literal["red", "green", "blue"] = "red",
132 ):
133 self.x = x
134 self.y = y
135 self.color = color
136
137 def __eq__(self, other):
138 if not isinstance(other, type(self)):
139 return False
140 return self.x == other.x and self.y == other.y and self.color == other.color
141
142
143 def test_bind_generic_class_accepts_default_multiple_args(
144 app, assert_parse_args, console, normalize_trailing_whitespace
145 ):
146 @app.command
147 def foo(coords: Coordinates, priority: int):
148 pass
149
150 assert_parse_args(foo, "foo 100 200 7", Coordinates(100, 200), 7)
151
152 with console.capture() as capture:
153 app("foo --help", console=console)
154
155 actual = capture.get()
156
157 expected = dedent(
158 """\
159 Usage: test_bind_generic_class foo [OPTIONS] COORDS.X COORDS.Y
160 PRIORITY
161
162 ╭─ Parameters ───────────────────────────────────────────────────────╮
163 │ * COORDS.X --coords.x [required] │
164 │ * COORDS.Y --coords.y [required] │
165 │ * PRIORITY --priority [required] │
166 │ --coords.color [choices: red, green, blue] [default: red] │
167 ╰────────────────────────────────────────────────────────────────────╯
168 """
169 )
170
171 assert normalize_trailing_whitespace(actual) == expected
172
173
174 def test_bind_generic_class_accepts_false_multiple_args(app, assert_parse_args, console):
175 @app.command
176 def foo(coords: Annotated[Coordinates, Parameter(accepts_keys=False)]):
177 pass
178
179 assert_parse_args(foo, "foo 100 200", Coordinates(100, 200))
180
181 with console.capture() as capture:
182 app("foo --help", console=console)
183
184 actual = capture.get()
185
186 expected = dedent(
187 """\
188 Usage: test_bind_generic_class foo COORDS
189
190 ╭─ Parameters ───────────────────────────────────────────────────────╮
191 │ * COORDS --coords [required] │
192 ╰────────────────────────────────────────────────────────────────────╯
193 """
194 )
195
196 assert actual == expected
197
198
199 def test_bind_generic_class_keyword_with_positional_only_subkeys(app, console, assert_parse_args):
200 """This test has a keyword-only parameter that has position-only subkeys, which are skipped."""
201
202 class User:
203 def __init__(self, name: str, age: int, /):
204 self.name = name
205 self.age = age
206
207 def __eq__(self, other):
208 if not isinstance(other, type(self)):
209 return False
210 return self.name == other.name and self.age == other.age
211
212 @app.command
213 def foo(*, user: User):
214 pass
215
216 assert_parse_args(foo, "foo --user Bob 30", user=User("Bob", 30))
217
218 with console.capture() as capture:
219 app("foo --help", console=console)
220
221 actual = capture.get()
222
223 expected = dedent(
224 """\
225 Usage: test_bind_generic_class foo --user USER
226
227 ╭─ Parameters ───────────────────────────────────────────────────────╮
228 │ * --user [required] │
229 ╰────────────────────────────────────────────────────────────────────╯
230 """
231 )
232 assert actual == expected
233
234 with pytest.raises(MissingArgumentError):
235 app("foo --user.name=Bob --user.age=100", exit_on_error=False)