| 1 |
from collections.abc import AsyncGenerator, Generator |
| 2 |
from functools import partial |
| 3 |
from typing import Annotated |
| 4 |
|
| 5 |
import pytest |
| 6 |
from fastapi import Depends, FastAPI |
| 7 |
from fastapi.testclient import TestClient |
| 8 |
|
| 9 |
app = FastAPI() |
| 10 |
|
| 11 |
|
| 12 |
def function_dependency(value: str) -> str: |
| 13 |
return value |
| 14 |
|
| 15 |
|
| 16 |
async def async_function_dependency(value: str) -> str: |
| 17 |
return value |
| 18 |
|
| 19 |
|
| 20 |
def gen_dependency(value: str) -> Generator[str, None, None]: |
| 21 |
yield value |
| 22 |
|
| 23 |
|
| 24 |
async def async_gen_dependency(value: str) -> AsyncGenerator[str, None]: |
| 25 |
yield value |
| 26 |
|
| 27 |
|
| 28 |
class CallableDependency: |
| 29 |
def __call__(self, value: str) -> str: |
| 30 |
return value |
| 31 |
|
| 32 |
|
| 33 |
class CallableGenDependency: |
| 34 |
def __call__(self, value: str) -> Generator[str, None, None]: |
| 35 |
yield value |
| 36 |
|
| 37 |
|
| 38 |
class AsyncCallableDependency: |
| 39 |
async def __call__(self, value: str) -> str: |
| 40 |
return value |
| 41 |
|
| 42 |
|
| 43 |
class AsyncCallableGenDependency: |
| 44 |
async def __call__(self, value: str) -> AsyncGenerator[str, None]: |
| 45 |
yield value |
| 46 |
|
| 47 |
|
| 48 |
class MethodsDependency: |
| 49 |
def synchronous(self, value: str) -> str: |
| 50 |
return value |
| 51 |
|
| 52 |
async def asynchronous(self, value: str) -> str: |
| 53 |
return value |
| 54 |
|
| 55 |
def synchronous_gen(self, value: str) -> Generator[str, None, None]: |
| 56 |
yield value |
| 57 |
|
| 58 |
async def asynchronous_gen(self, value: str) -> AsyncGenerator[str, None]: |
| 59 |
yield value |
| 60 |
|
| 61 |
|
| 62 |
callable_dependency = CallableDependency() |
| 63 |
callable_gen_dependency = CallableGenDependency() |
| 64 |
async_callable_dependency = AsyncCallableDependency() |
| 65 |
async_callable_gen_dependency = AsyncCallableGenDependency() |
| 66 |
methods_dependency = MethodsDependency() |
| 67 |
|
| 68 |
|
| 69 |
@app.get("/partial-function-dependency") |
| 70 |
async def get_partial_function_dependency( |
| 71 |
value: Annotated[ |
| 72 |
str, Depends(partial(function_dependency, "partial-function-dependency")) |
| 73 |
], |
| 74 |
) -> str: |
| 75 |
return value |
| 76 |
|
| 77 |
|
| 78 |
@app.get("/partial-async-function-dependency") |
| 79 |
async def get_partial_async_function_dependency( |
| 80 |
value: Annotated[ |
| 81 |
str, |
| 82 |
Depends( |
| 83 |
partial(async_function_dependency, "partial-async-function-dependency") |
| 84 |
), |
| 85 |
], |
| 86 |
) -> str: |
| 87 |
return value |
| 88 |
|
| 89 |
|
| 90 |
@app.get("/partial-gen-dependency") |
| 91 |
async def get_partial_gen_dependency( |
| 92 |
value: Annotated[str, Depends(partial(gen_dependency, "partial-gen-dependency"))], |
| 93 |
) -> str: |
| 94 |
return value |
| 95 |
|
| 96 |
|
| 97 |
@app.get("/partial-async-gen-dependency") |
| 98 |
async def get_partial_async_gen_dependency( |
| 99 |
value: Annotated[ |
| 100 |
str, Depends(partial(async_gen_dependency, "partial-async-gen-dependency")) |
| 101 |
], |
| 102 |
) -> str: |
| 103 |
return value |
| 104 |
|
| 105 |
|
| 106 |
@app.get("/partial-callable-dependency") |
| 107 |
async def get_partial_callable_dependency( |
| 108 |
value: Annotated[ |
| 109 |
str, Depends(partial(callable_dependency, "partial-callable-dependency")) |
| 110 |
], |
| 111 |
) -> str: |
| 112 |
return value |
| 113 |
|
| 114 |
|
| 115 |
@app.get("/partial-callable-gen-dependency") |
| 116 |
async def get_partial_callable_gen_dependency( |
| 117 |
value: Annotated[ |
| 118 |
str, |
| 119 |
Depends(partial(callable_gen_dependency, "partial-callable-gen-dependency")), |
| 120 |
], |
| 121 |
) -> str: |
| 122 |
return value |
| 123 |
|
| 124 |
|
| 125 |
@app.get("/partial-async-callable-dependency") |
| 126 |
async def get_partial_async_callable_dependency( |
| 127 |
value: Annotated[ |
| 128 |
str, |
| 129 |
Depends( |
| 130 |
partial(async_callable_dependency, "partial-async-callable-dependency") |
| 131 |
), |
| 132 |
], |
| 133 |
) -> str: |
| 134 |
return value |
| 135 |
|
| 136 |
|
| 137 |
@app.get("/partial-async-callable-gen-dependency") |
| 138 |
async def get_partial_async_callable_gen_dependency( |
| 139 |
value: Annotated[ |
| 140 |
str, |
| 141 |
Depends( |
| 142 |
partial( |
| 143 |
async_callable_gen_dependency, "partial-async-callable-gen-dependency" |
| 144 |
) |
| 145 |
), |
| 146 |
], |
| 147 |
) -> str: |
| 148 |
return value |
| 149 |
|
| 150 |
|
| 151 |
@app.get("/partial-synchronous-method-dependency") |
| 152 |
async def get_partial_synchronous_method_dependency( |
| 153 |
value: Annotated[ |
| 154 |
str, |
| 155 |
Depends( |
| 156 |
partial( |
| 157 |
methods_dependency.synchronous, "partial-synchronous-method-dependency" |
| 158 |
) |
| 159 |
), |
| 160 |
], |
| 161 |
) -> str: |
| 162 |
return value |
| 163 |
|
| 164 |
|
| 165 |
@app.get("/partial-synchronous-method-gen-dependency") |
| 166 |
async def get_partial_synchronous_method_gen_dependency( |
| 167 |
value: Annotated[ |
| 168 |
str, |
| 169 |
Depends( |
| 170 |
partial( |
| 171 |
methods_dependency.synchronous_gen, |
| 172 |
"partial-synchronous-method-gen-dependency", |
| 173 |
) |
| 174 |
), |
| 175 |
], |
| 176 |
) -> str: |
| 177 |
return value |
| 178 |
|
| 179 |
|
| 180 |
@app.get("/partial-asynchronous-method-dependency") |
| 181 |
async def get_partial_asynchronous_method_dependency( |
| 182 |
value: Annotated[ |
| 183 |
str, |
| 184 |
Depends( |
| 185 |
partial( |
| 186 |
methods_dependency.asynchronous, |
| 187 |
"partial-asynchronous-method-dependency", |
| 188 |
) |
| 189 |
), |
| 190 |
], |
| 191 |
) -> str: |
| 192 |
return value |
| 193 |
|
| 194 |
|
| 195 |
@app.get("/partial-asynchronous-method-gen-dependency") |
| 196 |
async def get_partial_asynchronous_method_gen_dependency( |
| 197 |
value: Annotated[ |
| 198 |
str, |
| 199 |
Depends( |
| 200 |
partial( |
| 201 |
methods_dependency.asynchronous_gen, |
| 202 |
"partial-asynchronous-method-gen-dependency", |
| 203 |
) |
| 204 |
), |
| 205 |
], |
| 206 |
) -> str: |
| 207 |
return value |
| 208 |
|
| 209 |
|
| 210 |
client = TestClient(app) |
| 211 |
|
| 212 |
|
| 213 |
@pytest.mark.parametrize( |
| 214 |
"route,value", |
| 215 |
[ |
| 216 |
("/partial-function-dependency", "partial-function-dependency"), |
| 217 |
( |
| 218 |
"/partial-async-function-dependency", |
| 219 |
"partial-async-function-dependency", |
| 220 |
), |
| 221 |
("/partial-gen-dependency", "partial-gen-dependency"), |
| 222 |
("/partial-async-gen-dependency", "partial-async-gen-dependency"), |
| 223 |
("/partial-callable-dependency", "partial-callable-dependency"), |
| 224 |
("/partial-callable-gen-dependency", "partial-callable-gen-dependency"), |
| 225 |
("/partial-async-callable-dependency", "partial-async-callable-dependency"), |
| 226 |
( |
| 227 |
"/partial-async-callable-gen-dependency", |
| 228 |
"partial-async-callable-gen-dependency", |
| 229 |
), |
| 230 |
( |
| 231 |
"/partial-synchronous-method-dependency", |
| 232 |
"partial-synchronous-method-dependency", |
| 233 |
), |
| 234 |
( |
| 235 |
"/partial-synchronous-method-gen-dependency", |
| 236 |
"partial-synchronous-method-gen-dependency", |
| 237 |
), |
| 238 |
( |
| 239 |
"/partial-asynchronous-method-dependency", |
| 240 |
"partial-asynchronous-method-dependency", |
| 241 |
), |
| 242 |
( |
| 243 |
"/partial-asynchronous-method-gen-dependency", |
| 244 |
"partial-asynchronous-method-gen-dependency", |
| 245 |
), |
| 246 |
], |
| 247 |
) |
| 248 |
def test_dependency_types_with_partial(route: str, value: str) -> None: |
| 249 |
response = client.get(route) |
| 250 |
assert response.status_code == 200, response.text |
| 251 |
assert response.json() == value |