taskiq-python/taskiq

Pylint and Mypy error in Middleware

realitix opened this issue · 2 comments

Hello,
When creating a middleware, if you put a function async, for example async def post_send, pylint will complain:

Method 'post_send' was expected to be 'non-async', found it instead as 'async' (invalid-overridden-method)

And mypy will complain:

error: Return type "Coroutine[Any, Any, Coroutine[Any, Any, None] | None]" of "post_send" incompatible with return type "Coroutine[Any, Any, None] | None" in supertype "TaskiqMiddleware"

Is it possible to fix this ? For example by adding directly in the Middleware base class the async methods.

I guess you incorrectly annotate them. Most probably you have something like this:

async def post_save(self, message: "TaskiqMessage", result: "TaskiqResult[Any]") -> Coroutine[Any, Any, None] | None:

But it should be:

async def post_save(self, message: "TaskiqMessage", result: "TaskiqResult[Any]") -> None:

# Or if you want to implement sync version
def post_save(self, message: "TaskiqMessage", result: "TaskiqResult[Any]") -> None:

Since taskiq allows these methods to be both sync and async, we have sync functions by default. Actually, async functions are simple sync functions that return coroutine objects which can be awaited. That's why we have sync functions in base class with ability to return either coroutine (for async functions) or None (for sync functions).

Indeed, thanks