Coverage for tests / test_dependency_partial.py: 100%

83 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from collections.abc import AsyncGenerator, Generator 1abcd

2from functools import partial 1abcd

3from typing import Annotated 1abcd

4 

5import pytest 1abcd

6from fastapi import Depends, FastAPI 1abcd

7from fastapi.testclient import TestClient 1abcd

8 

9app = FastAPI() 1abcd

10 

11 

12def function_dependency(value: str) -> str: 1abcd

13 return value 1efg

14 

15 

16async def async_function_dependency(value: str) -> str: 1abcd

17 return value 1efg

18 

19 

20def gen_dependency(value: str) -> Generator[str, None, None]: 1abcd

21 yield value 1efg

22 

23 

24async def async_gen_dependency(value: str) -> AsyncGenerator[str, None]: 1abcd

25 yield value 1efg

26 

27 

28class CallableDependency: 1abcd

29 def __call__(self, value: str) -> str: 1abcd

30 return value 1efg

31 

32 

33class CallableGenDependency: 1abcd

34 def __call__(self, value: str) -> Generator[str, None, None]: 1abcd

35 yield value 1efg

36 

37 

38class AsyncCallableDependency: 1abcd

39 async def __call__(self, value: str) -> str: 1abcd

40 return value 1efg

41 

42 

43class AsyncCallableGenDependency: 1abcd

44 async def __call__(self, value: str) -> AsyncGenerator[str, None]: 1abcd

45 yield value 1efg

46 

47 

48class MethodsDependency: 1abcd

49 def synchronous(self, value: str) -> str: 1abcd

50 return value 1efg

51 

52 async def asynchronous(self, value: str) -> str: 1abcd

53 return value 1efg

54 

55 def synchronous_gen(self, value: str) -> Generator[str, None, None]: 1abcd

56 yield value 1efg

57 

58 async def asynchronous_gen(self, value: str) -> AsyncGenerator[str, None]: 1abcd

59 yield value 1efg

60 

61 

62callable_dependency = CallableDependency() 1abcd

63callable_gen_dependency = CallableGenDependency() 1abcd

64async_callable_dependency = AsyncCallableDependency() 1abcd

65async_callable_gen_dependency = AsyncCallableGenDependency() 1abcd

66methods_dependency = MethodsDependency() 1abcd

67 

68 

69@app.get("/partial-function-dependency") 1abcd

70async def get_partial_function_dependency( 1abcd

71 value: Annotated[ 

72 str, Depends(partial(function_dependency, "partial-function-dependency")) 

73 ], 

74) -> str: 

75 return value 1efg

76 

77 

78@app.get("/partial-async-function-dependency") 1abcd

79async def get_partial_async_function_dependency( 1abcd

80 value: Annotated[ 

81 str, 

82 Depends( 

83 partial(async_function_dependency, "partial-async-function-dependency") 

84 ), 

85 ], 

86) -> str: 

87 return value 1efg

88 

89 

90@app.get("/partial-gen-dependency") 1abcd

91async def get_partial_gen_dependency( 1abcd

92 value: Annotated[str, Depends(partial(gen_dependency, "partial-gen-dependency"))], 

93) -> str: 

94 return value 1efg

95 

96 

97@app.get("/partial-async-gen-dependency") 1abcd

98async def get_partial_async_gen_dependency( 1abcd

99 value: Annotated[ 

100 str, Depends(partial(async_gen_dependency, "partial-async-gen-dependency")) 

101 ], 

102) -> str: 

103 return value 1efg

104 

105 

106@app.get("/partial-callable-dependency") 1abcd

107async def get_partial_callable_dependency( 1abcd

108 value: Annotated[ 

109 str, Depends(partial(callable_dependency, "partial-callable-dependency")) 

110 ], 

111) -> str: 

112 return value 1efg

113 

114 

115@app.get("/partial-callable-gen-dependency") 1abcd

116async def get_partial_callable_gen_dependency( 1abcd

117 value: Annotated[ 

118 str, 

119 Depends(partial(callable_gen_dependency, "partial-callable-gen-dependency")), 

120 ], 

121) -> str: 

122 return value 1efg

123 

124 

125@app.get("/partial-async-callable-dependency") 1abcd

126async def get_partial_async_callable_dependency( 1abcd

127 value: Annotated[ 

128 str, 

129 Depends( 

130 partial(async_callable_dependency, "partial-async-callable-dependency") 

131 ), 

132 ], 

133) -> str: 

134 return value 1efg

135 

136 

137@app.get("/partial-async-callable-gen-dependency") 1abcd

138async def get_partial_async_callable_gen_dependency( 1abcd

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 1efg

149 

150 

151@app.get("/partial-synchronous-method-dependency") 1abcd

152async def get_partial_synchronous_method_dependency( 1abcd

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 1efg

163 

164 

165@app.get("/partial-synchronous-method-gen-dependency") 1abcd

166async def get_partial_synchronous_method_gen_dependency( 1abcd

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 1efg

178 

179 

180@app.get("/partial-asynchronous-method-dependency") 1abcd

181async def get_partial_asynchronous_method_dependency( 1abcd

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 1efg

193 

194 

195@app.get("/partial-asynchronous-method-gen-dependency") 1abcd

196async def get_partial_asynchronous_method_gen_dependency( 1abcd

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 1efg

208 

209 

210client = TestClient(app) 1abcd

211 

212 

213@pytest.mark.parametrize( 1abcd

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) 

248def test_dependency_types_with_partial(route: str, value: str) -> None: 1abcd

249 response = client.get(route) 1efg

250 assert response.status_code == 200, response.text 1efg

251 assert response.json() == value 1efg