Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Return coroutines instead of awaitables from functions. #208

Merged
merged 1 commit into from
May 14, 2024

Conversation

DABND19
Copy link
Contributor

@DABND19 DABND19 commented May 7, 2024

Callable[..., Awaitable[T]] is not a coroutine function, in fact it should be Callable[..., Coroutine[Any, Any, T]].

If we have an interface like this:

import abc

class AbstractClient(abc.ABC):
    @abc.abstractmethod
    async def fetch(self) -> bytes:
        ...

Then for such an implementation, mypy will return an error:

import aiomisc

class Client(AbstractClient):
    @aiomisc.asyncretry(max_tries=3)
    async def fetch(self) -> bytes:
        ...

We can use workaround like this:

import abc
from typing import Awaitable

class AbstractClient(abc.ABC):
    @abc.abstractmethod
    def fetch(self) -> Awaitable[bytes]:
        ...

But in this case fetch is not a coroutine function. We will have problems using unittest.mock.MagicMock:

from unittest.mock import MagicMock

mocked_client = MagicMock(AbstractClient)
mocked_client.fetch.return_value = b'Hello, World!!!'

# will raise TypeError
await mocked_client.fetch()

@mosquito mosquito merged commit 9d80cb7 into aiokitchen:master May 14, 2024
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants