[Python] Is it safe to ignore the "Signature incompatible" error with mypy when overriding...

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a subclass of aiogram.Bot to implement rate limiting for messages. However, when I override the send_message method, I get a mypy error about signature incompatibility. Here's a minimal version of my code:

    from aiogram import Bot
    from aiogram.types import Message
    from aiolimiter import AsyncLimiter
    from typing import Any, Coroutine

    class RateLimitedBot(Bot):
    def __init__(self, token: str, rate: int = 30, per: float = 1, *args: Any, **kwargs: Any) -> None:
    super().__init__(token, *args, **kwargs)
    self.limiter = AsyncLimiter(rate, per)

    async def send_message(self, chat_id: int | str, text: str, **kwargs: Any) -> Coroutine[Any, Any, Message]:
    async with self.limiter:
    return await super().send_message(chat_id, text, **kwargs)


    The error is:

    Signature of "send_message" incompatible with supertype "Bot"


    The signature of the superclass Bot is:

    def send_message(self, chat_id: int | str, text: str,
    business_connection_id: str | None = ..., and a lot of parameters...) -> Coroutine[Any, Any, Message]



    Is it safe to ignore this mypy error, or is there a better way to handle it without breaking the type compatibility with aiogram?

    The code works. Perhaps there are some other ways to extend send_message functionality?

    Continue reading...

Compartilhe esta Página