Debian Codegraph @

tests/test_dependency_wrapped.py

raw
1 import inspect
2 import sys
3 from collections.abc import AsyncGenerator, Generator
4 from functools import wraps
5
6 import pytest
7 from fastapi import Depends, FastAPI
8 from fastapi.concurrency import iterate_in_threadpool, run_in_threadpool
9 from fastapi.testclient import TestClient
10
11 if sys.version_info >= (3, 13): # pragma: no cover
12 from inspect import iscoroutinefunction
13 else: # pragma: no cover
14 from asyncio import iscoroutinefunction
15
16
17 def noop_wrap(func):
18 @wraps(func)
19 def wrapper(*args, **kwargs):
20 return func(*args, **kwargs)
21
22 return wrapper
23
24
25 def noop_wrap_async(func):
26 if inspect.isgeneratorfunction(func):
27
28 @wraps(func)
29 async def gen_wrapper(*args, **kwargs):
30 async for item in iterate_in_threadpool(func(*args, **kwargs)):
31 yield item
32
33 return gen_wrapper
34
35 elif inspect.isasyncgenfunction(func):
36
37 @wraps(func)
38 async def async_gen_wrapper(*args, **kwargs):
39 async for item in func(*args, **kwargs):
40 yield item
41
42 return async_gen_wrapper
43
44 @wraps(func)
45 async def wrapper(*args, **kwargs):
46 if inspect.isroutine(func) and iscoroutinefunction(func):
47 return await func(*args, **kwargs)
48 if inspect.isclass(func):
49 return await run_in_threadpool(func, *args, **kwargs)
50 dunder_call = getattr(func, "__call__", None) # noqa: B004
51 if iscoroutinefunction(dunder_call):
52 return await dunder_call(*args, **kwargs)
53 return await run_in_threadpool(func, *args, **kwargs)
54
55 return wrapper
56
57
58 class ClassInstanceDep:
59 def __call__(self):
60 return True
61
62
63 class_instance_dep = ClassInstanceDep()
64 wrapped_class_instance_dep = noop_wrap(class_instance_dep)
65 wrapped_class_instance_dep_async_wrapper = noop_wrap_async(class_instance_dep)
66
67
68 class ClassInstanceGenDep:
69 def __call__(self):
70 yield True
71
72
73 class_instance_gen_dep = ClassInstanceGenDep()
74 wrapped_class_instance_gen_dep = noop_wrap(class_instance_gen_dep)
75
76
77 class ClassInstanceWrappedDep:
78 @noop_wrap
79 def __call__(self):
80 return True
81
82
83 class_instance_wrapped_dep = ClassInstanceWrappedDep()
84
85
86 class ClassInstanceWrappedAsyncDep:
87 @noop_wrap_async
88 def __call__(self):
89 return True
90
91
92 class_instance_wrapped_async_dep = ClassInstanceWrappedAsyncDep()
93
94
95 class ClassInstanceWrappedGenDep:
96 @noop_wrap
97 def __call__(self):
98 yield True
99
100
101 class_instance_wrapped_gen_dep = ClassInstanceWrappedGenDep()
102
103
104 class ClassInstanceWrappedAsyncGenDep:
105 @noop_wrap_async
106 def __call__(self):
107 yield True
108
109
110 class_instance_wrapped_async_gen_dep = ClassInstanceWrappedAsyncGenDep()
111
112
113 class ClassDep:
114 def __init__(self):
115 self.value = True
116
117
118 wrapped_class_dep = noop_wrap(ClassDep)
119 wrapped_class_dep_async_wrapper = noop_wrap_async(ClassDep)
120
121
122 class ClassInstanceAsyncDep:
123 async def __call__(self):
124 return True
125
126
127 class_instance_async_dep = ClassInstanceAsyncDep()
128 wrapped_class_instance_async_dep = noop_wrap(class_instance_async_dep)
129 wrapped_class_instance_async_dep_async_wrapper = noop_wrap_async(
130 class_instance_async_dep
131 )
132
133
134 class ClassInstanceAsyncGenDep:
135 async def __call__(self):
136 yield True
137
138
139 class_instance_async_gen_dep = ClassInstanceAsyncGenDep()
140 wrapped_class_instance_async_gen_dep = noop_wrap(class_instance_async_gen_dep)
141
142
143 class ClassInstanceAsyncWrappedDep:
144 @noop_wrap
145 async def __call__(self):
146 return True
147
148
149 class_instance_async_wrapped_dep = ClassInstanceAsyncWrappedDep()
150
151
152 class ClassInstanceAsyncWrappedAsyncDep:
153 @noop_wrap_async
154 async def __call__(self):
155 return True
156
157
158 class_instance_async_wrapped_async_dep = ClassInstanceAsyncWrappedAsyncDep()
159
160
161 class ClassInstanceAsyncWrappedGenDep:
162 @noop_wrap
163 async def __call__(self):
164 yield True
165
166
167 class_instance_async_wrapped_gen_dep = ClassInstanceAsyncWrappedGenDep()
168
169
170 class ClassInstanceAsyncWrappedGenAsyncDep:
171 @noop_wrap_async
172 async def __call__(self):
173 yield True
174
175
176 class_instance_async_wrapped_gen_async_dep = ClassInstanceAsyncWrappedGenAsyncDep()
177
178 app = FastAPI()
179
180 # Sync wrapper
181
182
183 @noop_wrap
184 def wrapped_dependency() -> bool:
185 return True
186
187
188 @noop_wrap
189 def wrapped_gen_dependency() -> Generator[bool, None, None]:
190 yield True
191
192
193 @noop_wrap
194 async def async_wrapped_dependency() -> bool:
195 return True
196
197
198 @noop_wrap
199 async def async_wrapped_gen_dependency() -> AsyncGenerator[bool, None]:
200 yield True
201
202
203 @app.get("/wrapped-dependency/")
204 async def get_wrapped_dependency(value: bool = Depends(wrapped_dependency)):
205 return value
206
207
208 @app.get("/wrapped-gen-dependency/")
209 async def get_wrapped_gen_dependency(value: bool = Depends(wrapped_gen_dependency)):
210 return value
211
212
213 @app.get("/async-wrapped-dependency/")
214 async def get_async_wrapped_dependency(value: bool = Depends(async_wrapped_dependency)):
215 return value
216
217
218 @app.get("/async-wrapped-gen-dependency/")
219 async def get_async_wrapped_gen_dependency(
220 value: bool = Depends(async_wrapped_gen_dependency),
221 ):
222 return value
223
224
225 @app.get("/wrapped-class-instance-dependency/")
226 async def get_wrapped_class_instance_dependency(
227 value: bool = Depends(wrapped_class_instance_dep),
228 ):
229 return value
230
231
232 @app.get("/wrapped-class-instance-async-dependency/")
233 async def get_wrapped_class_instance_async_dependency(
234 value: bool = Depends(wrapped_class_instance_async_dep),
235 ):
236 return value
237
238
239 @app.get("/wrapped-class-instance-gen-dependency/")
240 async def get_wrapped_class_instance_gen_dependency(
241 value: bool = Depends(wrapped_class_instance_gen_dep),
242 ):
243 return value
244
245
246 @app.get("/wrapped-class-instance-async-gen-dependency/")
247 async def get_wrapped_class_instance_async_gen_dependency(
248 value: bool = Depends(wrapped_class_instance_async_gen_dep),
249 ):
250 return value
251
252
253 @app.get("/class-instance-wrapped-dependency/")
254 async def get_class_instance_wrapped_dependency(
255 value: bool = Depends(class_instance_wrapped_dep),
256 ):
257 return value
258
259
260 @app.get("/class-instance-wrapped-async-dependency/")
261 async def get_class_instance_wrapped_async_dependency(
262 value: bool = Depends(class_instance_wrapped_async_dep),
263 ):
264 return value
265
266
267 @app.get("/class-instance-async-wrapped-dependency/")
268 async def get_class_instance_async_wrapped_dependency(
269 value: bool = Depends(class_instance_async_wrapped_dep),
270 ):
271 return value
272
273
274 @app.get("/class-instance-async-wrapped-async-dependency/")
275 async def get_class_instance_async_wrapped_async_dependency(
276 value: bool = Depends(class_instance_async_wrapped_async_dep),
277 ):
278 return value
279
280
281 @app.get("/class-instance-wrapped-gen-dependency/")
282 async def get_class_instance_wrapped_gen_dependency(
283 value: bool = Depends(class_instance_wrapped_gen_dep),
284 ):
285 return value
286
287
288 @app.get("/class-instance-wrapped-async-gen-dependency/")
289 async def get_class_instance_wrapped_async_gen_dependency(
290 value: bool = Depends(class_instance_wrapped_async_gen_dep),
291 ):
292 return value
293
294
295 @app.get("/class-instance-async-wrapped-gen-dependency/")
296 async def get_class_instance_async_wrapped_gen_dependency(
297 value: bool = Depends(class_instance_async_wrapped_gen_dep),
298 ):
299 return value
300
301
302 @app.get("/class-instance-async-wrapped-gen-async-dependency/")
303 async def get_class_instance_async_wrapped_gen_async_dependency(
304 value: bool = Depends(class_instance_async_wrapped_gen_async_dep),
305 ):
306 return value
307
308
309 @app.get("/wrapped-class-dependency/")
310 async def get_wrapped_class_dependency(value: ClassDep = Depends(wrapped_class_dep)):
311 return value.value
312
313
314 @app.get("/wrapped-endpoint/")
315 @noop_wrap
316 def get_wrapped_endpoint():
317 return True
318
319
320 @app.get("/async-wrapped-endpoint/")
321 @noop_wrap
322 async def get_async_wrapped_endpoint():
323 return True
324
325
326 # Async wrapper
327
328
329 @noop_wrap_async
330 def wrapped_dependency_async_wrapper() -> bool:
331 return True
332
333
334 @noop_wrap_async
335 def wrapped_gen_dependency_async_wrapper() -> Generator[bool, None, None]:
336 yield True
337
338
339 @noop_wrap_async
340 async def async_wrapped_dependency_async_wrapper() -> bool:
341 return True
342
343
344 @noop_wrap_async
345 async def async_wrapped_gen_dependency_async_wrapper() -> AsyncGenerator[bool, None]:
346 yield True
347
348
349 @app.get("/wrapped-dependency-async-wrapper/")
350 async def get_wrapped_dependency_async_wrapper(
351 value: bool = Depends(wrapped_dependency_async_wrapper),
352 ):
353 return value
354
355
356 @app.get("/wrapped-gen-dependency-async-wrapper/")
357 async def get_wrapped_gen_dependency_async_wrapper(
358 value: bool = Depends(wrapped_gen_dependency_async_wrapper),
359 ):
360 return value
361
362
363 @app.get("/async-wrapped-dependency-async-wrapper/")
364 async def get_async_wrapped_dependency_async_wrapper(
365 value: bool = Depends(async_wrapped_dependency_async_wrapper),
366 ):
367 return value
368
369
370 @app.get("/async-wrapped-gen-dependency-async-wrapper/")
371 async def get_async_wrapped_gen_dependency_async_wrapper(
372 value: bool = Depends(async_wrapped_gen_dependency_async_wrapper),
373 ):
374 return value
375
376
377 @app.get("/wrapped-class-instance-dependency-async-wrapper/")
378 async def get_wrapped_class_instance_dependency_async_wrapper(
379 value: bool = Depends(wrapped_class_instance_dep_async_wrapper),
380 ):
381 return value
382
383
384 @app.get("/wrapped-class-instance-async-dependency-async-wrapper/")
385 async def get_wrapped_class_instance_async_dependency_async_wrapper(
386 value: bool = Depends(wrapped_class_instance_async_dep_async_wrapper),
387 ):
388 return value
389
390
391 @app.get("/wrapped-class-dependency-async-wrapper/")
392 async def get_wrapped_class_dependency_async_wrapper(
393 value: ClassDep = Depends(wrapped_class_dep_async_wrapper),
394 ):
395 return value.value
396
397
398 @app.get("/wrapped-endpoint-async-wrapper/")
399 @noop_wrap_async
400 def get_wrapped_endpoint_async_wrapper():
401 return True
402
403
404 @app.get("/async-wrapped-endpoint-async-wrapper/")
405 @noop_wrap_async
406 async def get_async_wrapped_endpoint_async_wrapper():
407 return True
408
409
410 client = TestClient(app)
411
412
413 @pytest.mark.parametrize(
414 "route",
415 [
416 "/wrapped-dependency/",
417 "/wrapped-gen-dependency/",
418 "/async-wrapped-dependency/",
419 "/async-wrapped-gen-dependency/",
420 "/wrapped-class-instance-dependency/",
421 "/wrapped-class-instance-async-dependency/",
422 "/wrapped-class-instance-gen-dependency/",
423 "/wrapped-class-instance-async-gen-dependency/",
424 "/class-instance-wrapped-dependency/",
425 "/class-instance-wrapped-async-dependency/",
426 "/class-instance-async-wrapped-dependency/",
427 "/class-instance-async-wrapped-async-dependency/",
428 "/class-instance-wrapped-gen-dependency/",
429 "/class-instance-wrapped-async-gen-dependency/",
430 "/class-instance-async-wrapped-gen-dependency/",
431 "/class-instance-async-wrapped-gen-async-dependency/",
432 "/wrapped-class-dependency/",
433 "/wrapped-endpoint/",
434 "/async-wrapped-endpoint/",
435 "/wrapped-dependency-async-wrapper/",
436 "/wrapped-gen-dependency-async-wrapper/",
437 "/async-wrapped-dependency-async-wrapper/",
438 "/async-wrapped-gen-dependency-async-wrapper/",
439 "/wrapped-class-instance-dependency-async-wrapper/",
440 "/wrapped-class-instance-async-dependency-async-wrapper/",
441 "/wrapped-class-dependency-async-wrapper/",
442 "/wrapped-endpoint-async-wrapper/",
443 "/async-wrapped-endpoint-async-wrapper/",
444 ],
445 )
446 def test_class_dependency(route):
447 response = client.get(route)
448 assert response.status_code == 200, response.text
449 assert response.json() is True