fix(types): align request middleware typing with actual return value

Refs: #1723
This commit is contained in:
vmphase 2026-01-10 17:01:49 +02:00
parent ec7da0f678
commit 00026a29f5
4 changed files with 10 additions and 10 deletions

View file

@ -256,7 +256,7 @@ class BaseSession(abc.ABC):
timeout: int | None = None,
) -> TelegramType:
middleware = self.middleware.wrap_middlewares(self.make_request, timeout=timeout)
return cast(TelegramType, await middleware(bot, method))
return await middleware(bot, method)
async def __aenter__(self) -> Self:
return self

View file

@ -7,7 +7,7 @@ from aiogram.methods.base import TelegramType
if TYPE_CHECKING:
from aiogram.client.bot import Bot
from aiogram.methods import Response, TelegramMethod
from aiogram.methods import TelegramMethod
class NextRequestMiddlewareType(Protocol[TelegramType]): # pragma: no cover
@ -15,7 +15,7 @@ class NextRequestMiddlewareType(Protocol[TelegramType]): # pragma: no cover
self,
bot: Bot,
method: TelegramMethod[TelegramType],
) -> Response[TelegramType]:
) -> TelegramType:
pass
@ -25,7 +25,7 @@ class RequestMiddlewareType(Protocol): # pragma: no cover
make_request: NextRequestMiddlewareType[TelegramType],
bot: Bot,
method: TelegramMethod[TelegramType],
) -> Response[TelegramType]:
) -> TelegramType:
pass
@ -40,7 +40,7 @@ class BaseRequestMiddleware(ABC):
make_request: NextRequestMiddlewareType[TelegramType],
bot: Bot,
method: TelegramMethod[TelegramType],
) -> Response[TelegramType]:
) -> TelegramType:
"""
Execute middleware
@ -48,5 +48,5 @@ class BaseRequestMiddleware(ABC):
:param bot: bot for request making
:param method: Request method (Subclass of :class:`aiogram.methods.base.TelegramMethod`)
:return: :class:`aiogram.methods.Response`
:return: Concrete Telegram type (e.g. Message, User, etc.)
"""

View file

@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, Any
from aiogram import loggers
from aiogram.methods import TelegramMethod
from aiogram.methods.base import Response, TelegramType
from aiogram.methods.base import TelegramType
from .base import BaseRequestMiddleware, NextRequestMiddlewareType
@ -27,7 +27,7 @@ class RequestLogging(BaseRequestMiddleware):
make_request: NextRequestMiddlewareType[TelegramType],
bot: "Bot",
method: TelegramMethod[TelegramType],
) -> Response[TelegramType]:
) -> TelegramType:
if type(method) not in self.ignore_methods:
loggers.middlewares.info(
"Make request with method=%r by bot id=%d",

View file

@ -35,7 +35,7 @@ Register using decorator
make_request: NextRequestMiddlewareType[TelegramType],
bot: "Bot",
method: TelegramMethod[TelegramType],
) -> Response[TelegramType]:
) -> TelegramType:
# do something with request
return await make_request(bot, method)
@ -67,7 +67,7 @@ Function based session middleware
make_request: NextRequestMiddlewareType[TelegramType],
bot: "Bot",
method: TelegramMethod[TelegramType],
) -> Response[TelegramType]:
) -> TelegramType:
try:
# do something with request
return await make_request(bot, method)