From 30d7d056ccd2d5538e84338d61357d7eaa565854 Mon Sep 17 00:00:00 2001 From: Evgeny Seregin Date: Wed, 21 Aug 2024 18:25:39 +0400 Subject: [PATCH 1/4] fix: work with async generators --- fast_depends/core/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fast_depends/core/build.py b/fast_depends/core/build.py index 626edb0..38ef61a 100644 --- a/fast_depends/core/build.py +++ b/fast_depends/core/build.py @@ -55,7 +55,7 @@ def build_call_model( is_call_async = is_coroutine_callable(call) if is_sync is None: - is_sync = not is_call_async + is_sync = not is_call_async and not inspect.isasyncgenfunction(call) else: assert not ( is_sync and is_call_async From bd13af501c8a01f6f11c6faa94e5c0303e7f2bff Mon Sep 17 00:00:00 2001 From: Evgeny Seregin Date: Thu, 22 Aug 2024 12:25:30 +0400 Subject: [PATCH 2/4] tests: add test case with inject async func to async generator --- tests/async/test_depends.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/async/test_depends.py b/tests/async/test_depends.py index e2f5460..592436b 100644 --- a/tests/async/test_depends.py +++ b/tests/async/test_depends.py @@ -422,12 +422,19 @@ async def test_generator(): mock = Mock() async def func(): + mock.async_call() + + async def gen_func(): mock.start() yield mock.end() @inject - async def simple_func(a: str, d=Depends(func)) -> int: + async def simple_func( + a: str, + d=Depends(gen_func), + d2=Depends(func), + ) -> int: for _ in range(2): yield a @@ -436,6 +443,7 @@ async def simple_func(a: str, d=Depends(func)) -> int: assert not mock.end.called assert i == 1 + mock.async_call.assert_called_once() mock.end.assert_called_once() From 0ad15f158b5bdf3b990ae68bf914e7812cdc49a5 Mon Sep 17 00:00:00 2001 From: Pastukhov Nikita Date: Thu, 22 Aug 2024 14:40:51 +0300 Subject: [PATCH 3/4] Update build.py --- fast_depends/core/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fast_depends/core/build.py b/fast_depends/core/build.py index 38ef61a..839dee6 100644 --- a/fast_depends/core/build.py +++ b/fast_depends/core/build.py @@ -55,7 +55,7 @@ def build_call_model( is_call_async = is_coroutine_callable(call) if is_sync is None: - is_sync = not is_call_async and not inspect.isasyncgenfunction(call) + is_sync = not is_call_async and not is_async_gen_callable(call) else: assert not ( is_sync and is_call_async From 3c2105d5a7a14c7b2ce395e81f38254c85436bcb Mon Sep 17 00:00:00 2001 From: Pastukhov Nikita Date: Thu, 22 Aug 2024 14:46:04 +0300 Subject: [PATCH 4/4] Update build.py --- fast_depends/core/build.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fast_depends/core/build.py b/fast_depends/core/build.py index 839dee6..aedeaf7 100644 --- a/fast_depends/core/build.py +++ b/fast_depends/core/build.py @@ -53,9 +53,9 @@ def build_call_model( ) -> CallModel[P, T]: name = getattr(call, "__name__", type(call).__name__) - is_call_async = is_coroutine_callable(call) + is_call_async = is_coroutine_callable(call) or is_async_gen_callable(call) if is_sync is None: - is_sync = not is_call_async and not is_async_gen_callable(call) + is_sync = not is_call_async else: assert not ( is_sync and is_call_async