-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Allow to inject async functions into functions wrapped with asynccontextmanager #125
fix: Allow to inject async functions into functions wrapped with asynccontextmanager #125
Conversation
tests/async/test_depends.py
Outdated
|
||
@pytest.mark.anyio | ||
async def test_asynccontextmanager(): | ||
async def dep(a: str = "a"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, remove default value here and in sync test to make the test much representable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Thank you for the PRs a lot! Btw, what are u doing with FastDepends to find such bugs? 😅 |
I do nothing 😅 |
And thank you for your awesome projects. I use both FastDepends and FastStream and they are gorgeous |
@Zhenay I can suggest trying https://github.com/reagento/dishka/ |
@Tishka17 Thank you for your suggestion, but we are completely satisfied with FastDepends. |
When you try to inject synchronous or asynchronous dependencies into an asynchronous generator wrapped with
contextlib.asynccontextmanager
like this:you see the error
AttributeError: 'solve_async_gen' object has no attribute '_iter'
.The reverse order of the decorators works for sync dependincies only and raises the error
AssertionError: You cannot use async dependency `some_dep` at sync main
for async ones.This pull request allows the inject function be used in such context with the direct order of the decorators. I don't see any possibility to provide the ability to use the reverse order because of the way
asynccontextmanager
is implemented in the standard library:As you can see, the decorator returns the synchronous wrapper, so when fast_depends execute
build_call_model
it determines the function as synchronous.