This commit is contained in:
Michael 2026-04-04 17:10:08 +00:00 committed by GitHub
commit b516575759
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 11 additions and 10 deletions

1
CHANGES/1723.bugfix.rst Normal file
View file

@ -0,0 +1 @@
Fixed type hinting for session middlewares where the return type was incorrectly marked as :class:`aiogram.methods.base.Response` instead of the concrete :class:`aiogram.methods.base.TelegramType`.

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)