Repository navigation
fix: support async def views in inject and auto_inject - #40
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #39
injectnow checksinspect.iscoroutinefunction(func). For a coroutine function it returns anasync defwrapper that resolves the markers from the request's child container onflask.gand awaits the view. Flask sees a coroutine function and runs it throughensure_sync(asgiref). Sync views get the same wrapper as before.auto_inject=Truegoes through the sameinject, so it covers async views with no extra code, andmark_injectedis applied to both wrappers.Marker resolution inside the async wrapper stays sync, and the child container is closed with
close_sync()at teardown. Closing it withclose_asyncis out of scope per the issue, so the README now saysScope.REQUESTproviders with async finalizers are not supported.The async wrapper is cast to
Callable[..., T], sinceTis the coroutine type when the input is a coroutine function. That keeps the public signature unchanged and keeps ty happy.Tests (
tests/test_async_views.py, written first and seen failing with the "it was a coroutine" error):@injecton an async view returns 200 with the injected value, and the result is a coroutine functionauto_inject=Trueand no decoratorScope.REQUESTcached provider resolved in the async view is the same instance a sync@injecthelper resolves in that request, which shows the async wrapper reads that request's child container fromflask.gasgiref>=3.2(the floor of Flask'sasyncextra) is added to the dev group only. Runtime deps are unchanged.The matching note for the Flask page in the modern-di docs will go in a separate modern-di PR as a follow-up.