From 4118dadee39be634705125a03c55b0cd3643e3ca Mon Sep 17 00:00:00 2001 From: Artur Shiriev Date: Sat, 17 Oct 2026 20:59:36 +0300 Subject: [PATCH] fix: leave updates of an unknown type to aiogram --- modern_di_aiogram/main.py | 10 ++++++---- tests/test_inject.py | 12 +++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/modern_di_aiogram/main.py b/modern_di_aiogram/main.py index ac4881d..3704e31 100644 --- a/modern_di_aiogram/main.py +++ b/modern_di_aiogram/main.py @@ -1,4 +1,5 @@ import asyncio +import contextlib import functools import inspect import logging @@ -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 @@ -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: diff --git a/tests/test_inject.py b/tests/test_inject.py index e53588a..566ff11 100644 --- a/tests/test_inject.py +++ b/tests/test_inject.py @@ -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 @@ -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