-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Aiogram integration inject doesn't work with middlewares #185
Comments
Can we see a stacktrace? I do not see any problems from my side, |
I researched this and found that it originated from the Dispatcher side and because middleware uses @Inject on the call method. So this isn't a problem with feed_update method. Here is another mre that explains this problem: import asyncio
import datetime
from unittest.mock import AsyncMock
from aiogram import Dispatcher, BaseMiddleware
from aiogram.types import Update, Message, Chat
from dishka import make_async_container, FromDishka, Provider, Scope
from dishka.integrations.aiogram import setup_dishka, inject
class Some:
pass
class TestMiddleware(BaseMiddleware):
@inject
async def __call__(
self,
handler,
event,
data,
md: FromDishka[Some]
):
print(md)
return await handler(event, data)
async def start_bot():
provider = Provider()
provider.provide(source=lambda: Some(), provides=Some, scope=Scope.APP)
bot_container = make_async_container(provider)
dp = Dispatcher()
dp.update.outer_middleware(TestMiddleware())
bot = AsyncMock()
setup_dishka(router=dp, container=bot_container, auto_inject=True)
await dp.feed_update(
bot,
update=Update(
update_id=0,
message=Message(message_id=0, date=datetime.datetime.now(), chat=Chat(id=0, type="private"))
)
)
if __name__ == '__main__':
asyncio.run(start_bot()) Stacktrace:
|
Thanks, Middlewares in aiogram have different logic comparing to handlers so you cannot reuse I'll think about how can we add more automatic here, but it is more like enhancement than a bug |
def aiogram_middleware_inject(func):
return wrap_injection(
func=func,
is_async=True,
container_getter=lambda args, kwargs: args[3][CONTAINER_KEY],
) here is the finished inject decorator |
Description
As you know, aiogram integration, like any other, has a special function for preparing the main router to propagate the container into the handlers, middlewares, and etc. -
setup_dishka()
. But it works only with polling and webhooks, and not with manual update feeding viadp.feed_update
.Code example
Works good
And doesnt work:
It doesn't work because aiogram uses special kwargs on the
feed_update()
method and ignores dishka injection previously, as it is useful only for testing and some add-on libraries.The text was updated successfully, but these errors were encountered: