Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions modern_di_aiogram/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import contextlib
import functools
import inspect
import logging
Expand All @@ -8,6 +9,7 @@
from aiogram import BaseMiddleware, Dispatcher
from aiogram.dispatcher.event.handler import HandlerObject
from aiogram.types import TelegramObject, Update
from aiogram.types.update import UpdateTypeLookupError
from modern_di import Container, Scope, integrations, providers


Expand Down Expand Up @@ -48,12 +50,12 @@ async def __call__(
event: TelegramObject,
data: dict[str, typing.Any],
) -> typing.Any: # noqa: ANN401
context: dict[type[typing.Any], typing.Any] = {Update: event}
with contextlib.suppress(UpdateTypeLookupError):
context[TelegramObject] = typing.cast("Update", event).event
self.in_flight += 1
try:
async with self.container.build_child_container(
scope=Scope.REQUEST,
context={Update: event, TelegramObject: typing.cast("Update", event).event},
) as child_container:
async with self.container.build_child_container(scope=Scope.REQUEST, context=context) as child_container:
data[_CHILD_CONTAINER_KEY] = child_container
return await handler(event, data)
finally:
Expand Down
12 changes: 11 additions & 1 deletion tests/test_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import pytest
from aiogram import Bot, Dispatcher
from aiogram.types import Message
from aiogram.dispatcher.event.bases import UNHANDLED
from aiogram.types import Message, Update
from modern_di import Container, Group, Scope, providers

from modern_di_aiogram import FromDI, inject, setup_di
Expand Down Expand Up @@ -237,3 +238,12 @@ async def handler(
await dispatcher.emit_shutdown()

assert seen == {"bot": "original", "has_event_chat": True}


async def test_an_unknown_update_type_is_left_to_aiogram(dispatcher: Dispatcher, bot: Bot) -> None:
await dispatcher.emit_startup()
with pytest.warns(RuntimeWarning, match="Detected unknown update type"):
result = await dispatcher.feed_update(bot, Update(update_id=1))
await dispatcher.emit_shutdown()

assert result is UNHANDLED
Loading