asynctest icon indicating copy to clipboard operation
asynctest copied to clipboard

inspect.isawaitable() returns False when the argument is a CoroutineMock

Open thedrow opened this issue 6 years ago • 4 comments
trafficstars

Since a CoroutineMock is awaitable inspect.isawaitable() should return True. These are the criteria for an awaitable object.

thedrow avatar Jul 23 '19 07:07 thedrow

CoroutineMock should be named CoroutineFunctionMock.

inspect.isawaitable(my_coro_mock()) # True. This is intended.

Martiusweb avatar Jul 23 '19 08:07 Martiusweb

Here's a testcase:

In [2]: import inspect                                                                                                                                                                                      

In [3]: from asynctest import CoroutineMock                                                                                                                                                                 

In [4]: inspect.isawaitable(CoroutineMock())                                                                                                                                                                
Out[4]: False

I'm using Python 3.7.3.

thedrow avatar Jul 23 '19 09:07 thedrow

Yes, this is because CoroutineMock() is the mock of a coroutine function, which is not awaitable.

async def my_coro():
    ... # do something

inspect.isawaitable(my_coro)  # False
inspect.isawaitable(my_coro()) # True

and thus: inspect.iswawaitable(CoroutineMock) # False inspect.iswawaitable(CoroutineMock()) # False inspect.iswawaitable(CoroutineMock()()) # True

Note that this is not actually enforced by the unit tests.

Martiusweb avatar Jul 23 '19 09:07 Martiusweb

Re-opening to add unit tests to ensure CoroutineMock is tested against inspect.isawaitable().

Martiusweb avatar Jul 23 '19 09:07 Martiusweb