Repository navigation
fix: support sync handlers in inject and auto_inject - #46
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 #44
injectnow returns a sync wrapper for a function that is not a coroutine function and an async wrapper otherwise, the same split modern-di-flask has used since 4.1.0. Both wrappers find the per-update child container in the handler kwargs and resolveFromDIparameters from it the same way.Before this change the wrapper was always
async def. With@injecton a sync handler, aiogram saw a coroutine function, awaited the wrapper, and the wrapper then awaited the handler'sNonereturn value and raisedTypeError. Underauto_inject=True, the router walk swapped in the async wrapper but leftHandlerObject.awaitableasFalse, so aiogram ran the wrapper in a thread and dropped the coroutine. The handler never ran.The router walk now copies
awaitableandvarkwfrom the rebuiltHandlerObjectalong withcallbackandparams, so every field aiogram derives from the callback matches the new one.Typing:
injectis nowCallable[..., T] -> Callable[..., T], the same signature modern-di-flask uses. An async function'sTis its coroutine type, so the return type follows the input without overloads.modern_di_aiogram.dialog.injectis unchanged. aiogram-dialog awaits every getter and callback (Dialog._process_callback,SimpleEventProcessor.process_event,Dialog.load_data), so it does not support sync callbacks with or without this package. A sync wrapper there would fail the same way the async one does.Tests added, all failing before the fix:
auto_injectruns and gets its dependency, with warnings raised as errors so a "never awaited" warning fails the testHandlerObject.awaitablematches the swapped callback for both sync and async handlers afterauto_inject@injecton a sync handler runs it once and gets its dependency@injecton a sync handler forwards a declaredmodern_di_containerparameterinjectreturns a coroutine function only for coroutine functionsThe existing async tests cover the unchanged async behavior. README now says sync handlers are supported.
The
**kwargsparameter-order bug (#45) is not touched here.