taskiq-python / taskiq

Distributed task queue with full async support
MIT License
689 stars 44 forks source link

Pylint and Mypy error in Middleware #287

Closed realitix closed 4 months ago

realitix commented 4 months ago

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.

s3rius commented 4 months ago

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).

realitix commented 4 months ago

Indeed, thanks