From d689b007b77792628c3d58eefcf746914904d583 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 22 Jan 2024 12:13:03 +1100 Subject: [PATCH 01/10] Rename tests/test_dependency_class.py -> tests/test_dependency_types.py --- ...dency_class.py => test_dependency_types.py} | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) rename tests/{test_dependency_class.py => test_dependency_types.py} (95%) diff --git a/tests/test_dependency_class.py b/tests/test_dependency_types.py similarity index 95% rename from tests/test_dependency_class.py rename to tests/test_dependency_types.py index 0233492e6..a011e69e8 100644 --- a/tests/test_dependency_class.py +++ b/tests/test_dependency_types.py @@ -49,54 +49,54 @@ methods_dependency = MethodsDependency() @app.get("/callable-dependency") -async def get_callable_dependency(value: str = Depends(callable_dependency)): +async def get_callable_dependency(value: str = Depends(callable_dependency)) -> str: return value @app.get("/callable-gen-dependency") -async def get_callable_gen_dependency(value: str = Depends(callable_gen_dependency)): +async def get_callable_gen_dependency(value: str = Depends(callable_gen_dependency)) -> str: return value @app.get("/async-callable-dependency") async def get_async_callable_dependency( value: str = Depends(async_callable_dependency), -): +) -> str: return value @app.get("/async-callable-gen-dependency") async def get_async_callable_gen_dependency( value: str = Depends(async_callable_gen_dependency), -): +) -> str: return value @app.get("/synchronous-method-dependency") async def get_synchronous_method_dependency( value: str = Depends(methods_dependency.synchronous), -): +) -> str: return value @app.get("/synchronous-method-gen-dependency") async def get_synchronous_method_gen_dependency( value: str = Depends(methods_dependency.synchronous_gen), -): +) -> str: return value @app.get("/asynchronous-method-dependency") async def get_asynchronous_method_dependency( value: str = Depends(methods_dependency.asynchronous), -): +) -> str: return value @app.get("/asynchronous-method-gen-dependency") async def get_asynchronous_method_gen_dependency( value: str = Depends(methods_dependency.asynchronous_gen), -): +) -> str: return value @@ -116,7 +116,7 @@ client = TestClient(app) ("/asynchronous-method-gen-dependency", "asynchronous-method-gen-dependency"), ], ) -def test_class_dependency(route, value): +def test_class_dependency(route: str, value: str) -> None: response = client.get(route, params={"value": value}) assert response.status_code == 200, response.text assert response.json() == value From 57e17187388cce60e0d4cc33a0f9d8fd9e71846b Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 22 Jan 2024 12:13:05 +1100 Subject: [PATCH 02/10] Add test for sync/async function/gen dependencies --- tests/test_dependency_types.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/test_dependency_types.py b/tests/test_dependency_types.py index a011e69e8..3e4e5d7d1 100644 --- a/tests/test_dependency_types.py +++ b/tests/test_dependency_types.py @@ -7,6 +7,22 @@ from fastapi.testclient import TestClient app = FastAPI() +def function_dependency(value: str) -> str: + return value + + +async def async_function_dependency(value: str) -> str: + return value + + +def gen_dependency(value: str) -> str: + yield value + + +async def async_gen_dependency(value: str) -> str: + return value + + class CallableDependency: def __call__(self, value: str) -> str: return value @@ -106,6 +122,10 @@ client = TestClient(app) @pytest.mark.parametrize( "route,value", [ + ("/function-dependency", "function-dependency"), + ("/async-function-dependency", "async-function-dependency"), + ("/gen-dependency", "gen-dependency"), + ("/async-gen-dependency", "async-gen-dependency"), ("/callable-dependency", "callable-dependency"), ("/callable-gen-dependency", "callable-gen-dependency"), ("/async-callable-dependency", "async-callable-dependency"), @@ -116,7 +136,7 @@ client = TestClient(app) ("/asynchronous-method-gen-dependency", "asynchronous-method-gen-dependency"), ], ) -def test_class_dependency(route: str, value: str) -> None: +def test_dependency_types(route: str, value: str) -> None: response = client.get(route, params={"value": value}) assert response.status_code == 200, response.text assert response.json() == value From f16dd38b478c0a88ae4ae79fd73094fdb3ffa425 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 22 Jan 2024 12:13:05 +1100 Subject: [PATCH 03/10] Add support for functools.partial()-wrapped dependables --- fastapi/dependencies/utils.py | 13 ++-- tests/test_dependency_types.py | 116 +++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 4 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index b73473484..a08bb9742 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -1,6 +1,7 @@ import inspect from contextlib import AsyncExitStack, contextmanager from copy import deepcopy +from functools import partial from typing import ( Any, Callable, @@ -487,10 +488,10 @@ def add_param_to_fields(*, field: ModelField, dependant: Dependant) -> None: def is_coroutine_callable(call: Callable[..., Any]) -> bool: - if inspect.isroutine(call): - return inspect.iscoroutinefunction(call) - if inspect.isclass(call): - return False + if inspect.iscoroutinefunction(call): + return True + if isinstance(call, partial): + return is_coroutine_callable(call.func) dunder_call = getattr(call, "__call__", None) # noqa: B004 return inspect.iscoroutinefunction(dunder_call) @@ -498,6 +499,8 @@ def is_coroutine_callable(call: Callable[..., Any]) -> bool: def is_async_gen_callable(call: Callable[..., Any]) -> bool: if inspect.isasyncgenfunction(call): return True + if isinstance(call, partial): + return is_async_gen_callable(call.func) dunder_call = getattr(call, "__call__", None) # noqa: B004 return inspect.isasyncgenfunction(dunder_call) @@ -505,6 +508,8 @@ def is_async_gen_callable(call: Callable[..., Any]) -> bool: def is_gen_callable(call: Callable[..., Any]) -> bool: if inspect.isgeneratorfunction(call): return True + if isinstance(call, partial): + return is_gen_callable(call.func) dunder_call = getattr(call, "__call__", None) # noqa: B004 return inspect.isgeneratorfunction(dunder_call) diff --git a/tests/test_dependency_types.py b/tests/test_dependency_types.py index 3e4e5d7d1..4bd37afa5 100644 --- a/tests/test_dependency_types.py +++ b/tests/test_dependency_types.py @@ -1,4 +1,5 @@ from typing import AsyncGenerator, Generator +from functools import partial import pytest from fastapi import Depends, FastAPI @@ -64,6 +65,26 @@ async_callable_gen_dependency = AsyncCallableGenDependency() methods_dependency = MethodsDependency() +@app.get("/function-dependency") +async def get_function_dependency(value: str = Depends(function_dependency)) -> str: + return value + + +@app.get("/async-function-dependency") +async def get_async_function_dependency(value: str = Depends(async_function_dependency)) -> str: + return value + + +@app.get("/gen-dependency") +async def get_gen_dependency(value: str = Depends(gen_dependency)) -> str: + return value + + +@app.get("/async-gen-dependency") +async def get_async_gen_dependency(value: str = Depends(async_gen_dependency)) -> str: + return value + + @app.get("/callable-dependency") async def get_callable_dependency(value: str = Depends(callable_dependency)) -> str: return value @@ -116,6 +137,78 @@ async def get_asynchronous_method_gen_dependency( return value +@app.get("/partial-function-dependency") +async def get_partial_function_dependency(value: str = Depends(partial(function_dependency, "partial-function-dependency"))) -> str: + return value + + +@app.get("/partial-async-function-dependency") +async def get_partial_async_function_dependency(value: str = Depends(partial(async_function_dependency, "partial-async-function-dependency"))) -> str: + return value + + +@app.get("/partial-gen-dependency") +async def get_partial_gen_dependency(value: str = Depends(partial(gen_dependency, "partial-gen-dependency"))) -> str: + return value + + +@app.get("/partial-async-gen-dependency") +async def get_partial_async_gen_dependency(value: str = Depends(partial(async_gen_dependency, "partial-async-gen-dependency"))) -> str: + return value + + +@app.get("/partial-callable-dependency") +async def get_partial_callable_dependency(value: str = Depends(partial(callable_dependency, "partial-callable-dependency"))) -> str: + return value + + +@app.get("/partial-callable-gen-dependency") +async def get_partial_callable_gen_dependency(value: str = Depends(partial(callable_gen_dependency, "partial-callable-gen-dependency"))) -> str: + return value + + +@app.get("/partial-async-callable-dependency") +async def get_partial_async_callable_dependency( + value: str = Depends(partial(async_callable_dependency, "partial-async-callable-dependency")), +) -> str: + return value + + +@app.get("/partial-async-callable-gen-dependency") +async def get_partial_async_callable_gen_dependency( + value: str = Depends(partial(async_callable_gen_dependency, "partial-async-callable-gen-dependency")), +) -> str: + return value + + +@app.get("/partial-synchronous-method-dependency") +async def get_partial_synchronous_method_dependency( + value: str = Depends(partial(methods_dependency.synchronous, "partial-synchronous-method-dependency")), +) -> str: + return value + + +@app.get("/partial-synchronous-method-gen-dependency") +async def get_partial_synchronous_method_gen_dependency( + value: str = Depends(partial(methods_dependency.synchronous_gen, "partial-synchronous-method-gen-dependency")), +) -> str: + return value + + +@app.get("/partial-asynchronous-method-dependency") +async def get_partial_asynchronous_method_dependency( + value: str = Depends(partial(methods_dependency.asynchronous, "partial-asynchronous-method-dependency")), +) -> str: + return value + + +@app.get("/partial-asynchronous-method-gen-dependency") +async def get_partial_asynchronous_method_gen_dependency( + value: str = Depends(partial(methods_dependency.asynchronous_gen, "partial-asynchronous-method-gen-dependency")), +) -> str: + return value + + client = TestClient(app) @@ -140,3 +233,26 @@ def test_dependency_types(route: str, value: str) -> None: response = client.get(route, params={"value": value}) assert response.status_code == 200, response.text assert response.json() == value + + +@pytest.mark.parametrize( + "route,value", + [ + ("/partial-function-dependency", "partial-function-dependency"), + ("/partial-async-function-dependency", "partial-async-function-dependency"), + ("/partial-gen-dependency", "partial-gen-dependency"), + ("/partial-async-gen-dependency", "partial-async-gen-dependency"), + ("/partial-callable-dependency", "partial-callable-dependency"), + ("/partial-callable-gen-dependency", "partial-callable-gen-dependency"), + ("/partial-async-callable-dependency", "partial-async-callable-dependency"), + ("/partial-async-callable-gen-dependency", "partial-async-callable-gen-dependency"), + ("/partial-synchronous-method-dependency", "partial-synchronous-method-dependency"), + ("/partial-synchronous-method-gen-dependency", "partial-synchronous-method-gen-dependency"), + ("/partial-asynchronous-method-dependency", "partial-asynchronous-method-dependency"), + ("/partial-asynchronous-method-gen-dependency", "partial-asynchronous-method-gen-dependency"), + ], +) +def test_dependency_types_with_partial(route: str, value: str) -> None: + response = client.get(route) + assert response.status_code == 200, response.text + assert response.json() == value From 3ba104e6d7d223c20c307b45f4ab49eba088c131 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Jan 2024 12:12:24 +1100 Subject: [PATCH 04/10] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_dependency_types.py | 95 +++++++++++++++++++++++++++------- 1 file changed, 75 insertions(+), 20 deletions(-) diff --git a/tests/test_dependency_types.py b/tests/test_dependency_types.py index 4bd37afa5..d4bf99061 100644 --- a/tests/test_dependency_types.py +++ b/tests/test_dependency_types.py @@ -1,5 +1,5 @@ -from typing import AsyncGenerator, Generator from functools import partial +from typing import AsyncGenerator, Generator import pytest from fastapi import Depends, FastAPI @@ -71,7 +71,9 @@ async def get_function_dependency(value: str = Depends(function_dependency)) -> @app.get("/async-function-dependency") -async def get_async_function_dependency(value: str = Depends(async_function_dependency)) -> str: +async def get_async_function_dependency( + value: str = Depends(async_function_dependency), +) -> str: return value @@ -91,7 +93,9 @@ async def get_callable_dependency(value: str = Depends(callable_dependency)) -> @app.get("/callable-gen-dependency") -async def get_callable_gen_dependency(value: str = Depends(callable_gen_dependency)) -> str: +async def get_callable_gen_dependency( + value: str = Depends(callable_gen_dependency), +) -> str: return value @@ -138,73 +142,109 @@ async def get_asynchronous_method_gen_dependency( @app.get("/partial-function-dependency") -async def get_partial_function_dependency(value: str = Depends(partial(function_dependency, "partial-function-dependency"))) -> str: +async def get_partial_function_dependency( + value: str = Depends(partial(function_dependency, "partial-function-dependency")) +) -> str: return value @app.get("/partial-async-function-dependency") -async def get_partial_async_function_dependency(value: str = Depends(partial(async_function_dependency, "partial-async-function-dependency"))) -> str: +async def get_partial_async_function_dependency( + value: str = Depends( + partial(async_function_dependency, "partial-async-function-dependency") + ) +) -> str: return value @app.get("/partial-gen-dependency") -async def get_partial_gen_dependency(value: str = Depends(partial(gen_dependency, "partial-gen-dependency"))) -> str: +async def get_partial_gen_dependency( + value: str = Depends(partial(gen_dependency, "partial-gen-dependency")) +) -> str: return value @app.get("/partial-async-gen-dependency") -async def get_partial_async_gen_dependency(value: str = Depends(partial(async_gen_dependency, "partial-async-gen-dependency"))) -> str: +async def get_partial_async_gen_dependency( + value: str = Depends(partial(async_gen_dependency, "partial-async-gen-dependency")) +) -> str: return value @app.get("/partial-callable-dependency") -async def get_partial_callable_dependency(value: str = Depends(partial(callable_dependency, "partial-callable-dependency"))) -> str: +async def get_partial_callable_dependency( + value: str = Depends(partial(callable_dependency, "partial-callable-dependency")) +) -> str: return value @app.get("/partial-callable-gen-dependency") -async def get_partial_callable_gen_dependency(value: str = Depends(partial(callable_gen_dependency, "partial-callable-gen-dependency"))) -> str: +async def get_partial_callable_gen_dependency( + value: str = Depends( + partial(callable_gen_dependency, "partial-callable-gen-dependency") + ) +) -> str: return value @app.get("/partial-async-callable-dependency") async def get_partial_async_callable_dependency( - value: str = Depends(partial(async_callable_dependency, "partial-async-callable-dependency")), + value: str = Depends( + partial(async_callable_dependency, "partial-async-callable-dependency") + ), ) -> str: return value @app.get("/partial-async-callable-gen-dependency") async def get_partial_async_callable_gen_dependency( - value: str = Depends(partial(async_callable_gen_dependency, "partial-async-callable-gen-dependency")), + value: str = Depends( + partial(async_callable_gen_dependency, "partial-async-callable-gen-dependency") + ), ) -> str: return value @app.get("/partial-synchronous-method-dependency") async def get_partial_synchronous_method_dependency( - value: str = Depends(partial(methods_dependency.synchronous, "partial-synchronous-method-dependency")), + value: str = Depends( + partial(methods_dependency.synchronous, "partial-synchronous-method-dependency") + ), ) -> str: return value @app.get("/partial-synchronous-method-gen-dependency") async def get_partial_synchronous_method_gen_dependency( - value: str = Depends(partial(methods_dependency.synchronous_gen, "partial-synchronous-method-gen-dependency")), + value: str = Depends( + partial( + methods_dependency.synchronous_gen, + "partial-synchronous-method-gen-dependency", + ) + ), ) -> str: return value @app.get("/partial-asynchronous-method-dependency") async def get_partial_asynchronous_method_dependency( - value: str = Depends(partial(methods_dependency.asynchronous, "partial-asynchronous-method-dependency")), + value: str = Depends( + partial( + methods_dependency.asynchronous, "partial-asynchronous-method-dependency" + ) + ), ) -> str: return value @app.get("/partial-asynchronous-method-gen-dependency") async def get_partial_asynchronous_method_gen_dependency( - value: str = Depends(partial(methods_dependency.asynchronous_gen, "partial-asynchronous-method-gen-dependency")), + value: str = Depends( + partial( + methods_dependency.asynchronous_gen, + "partial-asynchronous-method-gen-dependency", + ) + ), ) -> str: return value @@ -245,11 +285,26 @@ def test_dependency_types(route: str, value: str) -> None: ("/partial-callable-dependency", "partial-callable-dependency"), ("/partial-callable-gen-dependency", "partial-callable-gen-dependency"), ("/partial-async-callable-dependency", "partial-async-callable-dependency"), - ("/partial-async-callable-gen-dependency", "partial-async-callable-gen-dependency"), - ("/partial-synchronous-method-dependency", "partial-synchronous-method-dependency"), - ("/partial-synchronous-method-gen-dependency", "partial-synchronous-method-gen-dependency"), - ("/partial-asynchronous-method-dependency", "partial-asynchronous-method-dependency"), - ("/partial-asynchronous-method-gen-dependency", "partial-asynchronous-method-gen-dependency"), + ( + "/partial-async-callable-gen-dependency", + "partial-async-callable-gen-dependency", + ), + ( + "/partial-synchronous-method-dependency", + "partial-synchronous-method-dependency", + ), + ( + "/partial-synchronous-method-gen-dependency", + "partial-synchronous-method-gen-dependency", + ), + ( + "/partial-asynchronous-method-dependency", + "partial-asynchronous-method-dependency", + ), + ( + "/partial-asynchronous-method-gen-dependency", + "partial-asynchronous-method-gen-dependency", + ), ], ) def test_dependency_types_with_partial(route: str, value: str) -> None: From 05ab6993162014e58e2078421e574217194ce619 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 22 Jan 2024 12:13:06 +1100 Subject: [PATCH 05/10] Fix async_gen_dependency() should use yield; also fix type annotation --- tests/test_dependency_types.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_dependency_types.py b/tests/test_dependency_types.py index d4bf99061..dd1741421 100644 --- a/tests/test_dependency_types.py +++ b/tests/test_dependency_types.py @@ -16,12 +16,12 @@ async def async_function_dependency(value: str) -> str: return value -def gen_dependency(value: str) -> str: +def gen_dependency(value: str) -> Generator[str, None, None]: yield value -async def async_gen_dependency(value: str) -> str: - return value +async def async_gen_dependency(value: str) -> AsyncGenerator[str, None]: + yield value class CallableDependency: From b593e4991c5af5e6474c5f9b137ed6a390128d69 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Jan 2024 01:15:07 +0000 Subject: [PATCH 06/10] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_dependency_types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_dependency_types.py b/tests/test_dependency_types.py index dd1741421..ace41603f 100644 --- a/tests/test_dependency_types.py +++ b/tests/test_dependency_types.py @@ -152,7 +152,7 @@ async def get_partial_function_dependency( async def get_partial_async_function_dependency( value: str = Depends( partial(async_function_dependency, "partial-async-function-dependency") - ) + ), ) -> str: return value @@ -182,7 +182,7 @@ async def get_partial_callable_dependency( async def get_partial_callable_gen_dependency( value: str = Depends( partial(callable_gen_dependency, "partial-callable-gen-dependency") - ) + ), ) -> str: return value From a6e9c039808807db5bc445caed176a8da771f31d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Jun 2025 20:09:05 +0000 Subject: [PATCH 07/10] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_dependency_types.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_dependency_types.py b/tests/test_dependency_types.py index ace41603f..6e22dd811 100644 --- a/tests/test_dependency_types.py +++ b/tests/test_dependency_types.py @@ -143,7 +143,7 @@ async def get_asynchronous_method_gen_dependency( @app.get("/partial-function-dependency") async def get_partial_function_dependency( - value: str = Depends(partial(function_dependency, "partial-function-dependency")) + value: str = Depends(partial(function_dependency, "partial-function-dependency")), ) -> str: return value @@ -159,21 +159,21 @@ async def get_partial_async_function_dependency( @app.get("/partial-gen-dependency") async def get_partial_gen_dependency( - value: str = Depends(partial(gen_dependency, "partial-gen-dependency")) + value: str = Depends(partial(gen_dependency, "partial-gen-dependency")), ) -> str: return value @app.get("/partial-async-gen-dependency") async def get_partial_async_gen_dependency( - value: str = Depends(partial(async_gen_dependency, "partial-async-gen-dependency")) + value: str = Depends(partial(async_gen_dependency, "partial-async-gen-dependency")), ) -> str: return value @app.get("/partial-callable-dependency") async def get_partial_callable_dependency( - value: str = Depends(partial(callable_dependency, "partial-callable-dependency")) + value: str = Depends(partial(callable_dependency, "partial-callable-dependency")), ) -> str: return value From 9a434c44904716363c9ed01fac7ab3ccfc931ce6 Mon Sep 17 00:00:00 2001 From: Yurii Motov Date: Sun, 6 Jul 2025 15:36:10 +0200 Subject: [PATCH 08/10] Restore `test_dependency_class.py` --- tests/test_dependency_class.py | 122 +++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tests/test_dependency_class.py diff --git a/tests/test_dependency_class.py b/tests/test_dependency_class.py new file mode 100644 index 000000000..0c7b79744 --- /dev/null +++ b/tests/test_dependency_class.py @@ -0,0 +1,122 @@ +from typing import AsyncGenerator, Generator + +import pytest +from fastapi import Depends, FastAPI +from fastapi.testclient import TestClient + +app = FastAPI() + + +class CallableDependency: + def __call__(self, value: str) -> str: + return value + + +class CallableGenDependency: + def __call__(self, value: str) -> Generator[str, None, None]: + yield value + + +class AsyncCallableDependency: + async def __call__(self, value: str) -> str: + return value + + +class AsyncCallableGenDependency: + async def __call__(self, value: str) -> AsyncGenerator[str, None]: + yield value + + +class MethodsDependency: + def synchronous(self, value: str) -> str: + return value + + async def asynchronous(self, value: str) -> str: + return value + + def synchronous_gen(self, value: str) -> Generator[str, None, None]: + yield value + + async def asynchronous_gen(self, value: str) -> AsyncGenerator[str, None]: + yield value + + +callable_dependency = CallableDependency() +callable_gen_dependency = CallableGenDependency() +async_callable_dependency = AsyncCallableDependency() +async_callable_gen_dependency = AsyncCallableGenDependency() +methods_dependency = MethodsDependency() + + +@app.get("/callable-dependency") +async def get_callable_dependency(value: str = Depends(callable_dependency)): + return value + + +@app.get("/callable-gen-dependency") +async def get_callable_gen_dependency(value: str = Depends(callable_gen_dependency)): + return value + + +@app.get("/async-callable-dependency") +async def get_async_callable_dependency( + value: str = Depends(async_callable_dependency), +): + return value + + +@app.get("/async-callable-gen-dependency") +async def get_async_callable_gen_dependency( + value: str = Depends(async_callable_gen_dependency), +): + return value + + +@app.get("/synchronous-method-dependency") +async def get_synchronous_method_dependency( + value: str = Depends(methods_dependency.synchronous), +): + return value + + +@app.get("/synchronous-method-gen-dependency") +async def get_synchronous_method_gen_dependency( + value: str = Depends(methods_dependency.synchronous_gen), +): + return value + + +@app.get("/asynchronous-method-dependency") +async def get_asynchronous_method_dependency( + value: str = Depends(methods_dependency.asynchronous), +): + return value + + +@app.get("/asynchronous-method-gen-dependency") +async def get_asynchronous_method_gen_dependency( + value: str = Depends(methods_dependency.asynchronous_gen), +): + return value + + +client = TestClient(app) + + +@pytest.mark.parametrize( + "route,value", + [ + ("/callable-dependency", "callable-dependency"), + ("/callable-gen-dependency", "callable-gen-dependency"), + ("/async-callable-dependency", "async-callable-dependency"), + ("/async-callable-gen-dependency", "async-callable-gen-dependency"), + ("/synchronous-method-dependency", "synchronous-method-dependency"), + ("/synchronous-method-gen-dependency", "synchronous-method-gen-dependency"), + ("/asynchronous-method-dependency", "asynchronous-method-dependency"), + ("/asynchronous-method-gen-dependency", "asynchronous-method-gen-dependency"), + ], +) +def test_class_dependency(route, value): + response = client.get(route, params={"value": value}) + assert response.status_code == 200, response.text + assert response.json() == value \ No newline at end of file From ce77af922af66e7045e0437ea95398796c4c910f Mon Sep 17 00:00:00 2001 From: Yurii Motov Date: Sun, 6 Jul 2025 15:40:25 +0200 Subject: [PATCH 09/10] Clean up tests for partial dependencies and rename to `test_dependency_partial.py` --- ...cy_types.py => test_dependency_partial.py} | 104 +----------------- 1 file changed, 4 insertions(+), 100 deletions(-) rename tests/{test_dependency_types.py => test_dependency_partial.py} (65%) diff --git a/tests/test_dependency_types.py b/tests/test_dependency_partial.py similarity index 65% rename from tests/test_dependency_types.py rename to tests/test_dependency_partial.py index 6e22dd811..5caffeec1 100644 --- a/tests/test_dependency_types.py +++ b/tests/test_dependency_partial.py @@ -65,82 +65,6 @@ async_callable_gen_dependency = AsyncCallableGenDependency() methods_dependency = MethodsDependency() -@app.get("/function-dependency") -async def get_function_dependency(value: str = Depends(function_dependency)) -> str: - return value - - -@app.get("/async-function-dependency") -async def get_async_function_dependency( - value: str = Depends(async_function_dependency), -) -> str: - return value - - -@app.get("/gen-dependency") -async def get_gen_dependency(value: str = Depends(gen_dependency)) -> str: - return value - - -@app.get("/async-gen-dependency") -async def get_async_gen_dependency(value: str = Depends(async_gen_dependency)) -> str: - return value - - -@app.get("/callable-dependency") -async def get_callable_dependency(value: str = Depends(callable_dependency)) -> str: - return value - - -@app.get("/callable-gen-dependency") -async def get_callable_gen_dependency( - value: str = Depends(callable_gen_dependency), -) -> str: - return value - - -@app.get("/async-callable-dependency") -async def get_async_callable_dependency( - value: str = Depends(async_callable_dependency), -) -> str: - return value - - -@app.get("/async-callable-gen-dependency") -async def get_async_callable_gen_dependency( - value: str = Depends(async_callable_gen_dependency), -) -> str: - return value - - -@app.get("/synchronous-method-dependency") -async def get_synchronous_method_dependency( - value: str = Depends(methods_dependency.synchronous), -) -> str: - return value - - -@app.get("/synchronous-method-gen-dependency") -async def get_synchronous_method_gen_dependency( - value: str = Depends(methods_dependency.synchronous_gen), -) -> str: - return value - - -@app.get("/asynchronous-method-dependency") -async def get_asynchronous_method_dependency( - value: str = Depends(methods_dependency.asynchronous), -) -> str: - return value - - -@app.get("/asynchronous-method-gen-dependency") -async def get_asynchronous_method_gen_dependency( - value: str = Depends(methods_dependency.asynchronous_gen), -) -> str: - return value - - @app.get("/partial-function-dependency") async def get_partial_function_dependency( value: str = Depends(partial(function_dependency, "partial-function-dependency")), @@ -252,34 +176,14 @@ async def get_partial_asynchronous_method_gen_dependency( client = TestClient(app) -@pytest.mark.parametrize( - "route,value", - [ - ("/function-dependency", "function-dependency"), - ("/async-function-dependency", "async-function-dependency"), - ("/gen-dependency", "gen-dependency"), - ("/async-gen-dependency", "async-gen-dependency"), - ("/callable-dependency", "callable-dependency"), - ("/callable-gen-dependency", "callable-gen-dependency"), - ("/async-callable-dependency", "async-callable-dependency"), - ("/async-callable-gen-dependency", "async-callable-gen-dependency"), - ("/synchronous-method-dependency", "synchronous-method-dependency"), - ("/synchronous-method-gen-dependency", "synchronous-method-gen-dependency"), - ("/asynchronous-method-dependency", "asynchronous-method-dependency"), - ("/asynchronous-method-gen-dependency", "asynchronous-method-gen-dependency"), - ], -) -def test_dependency_types(route: str, value: str) -> None: - response = client.get(route, params={"value": value}) - assert response.status_code == 200, response.text - assert response.json() == value - - @pytest.mark.parametrize( "route,value", [ ("/partial-function-dependency", "partial-function-dependency"), - ("/partial-async-function-dependency", "partial-async-function-dependency"), + ( + "/partial-async-function-dependency", + "partial-async-function-dependency", + ), ("/partial-gen-dependency", "partial-gen-dependency"), ("/partial-async-gen-dependency", "partial-async-gen-dependency"), ("/partial-callable-dependency", "partial-callable-dependency"), From 41d7016a3787cd69a4b05a556657358ea1ac9096 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Jul 2025 13:42:10 +0000 Subject: [PATCH 10/10] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_dependency_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_dependency_class.py b/tests/test_dependency_class.py index 0c7b79744..0233492e6 100644 --- a/tests/test_dependency_class.py +++ b/tests/test_dependency_class.py @@ -119,4 +119,4 @@ client = TestClient(app) def test_class_dependency(route, value): response = client.get(route, params={"value": value}) assert response.status_code == 200, response.text - assert response.json() == value \ No newline at end of file + assert response.json() == value