asynctest icon indicating copy to clipboard operation
asynctest copied to clipboard

Return value of CoroutineMock is not an instance of abc.Coroutine

Open thedrow opened this issue 6 years ago • 2 comments

An awaitable should always return a coroutine and thus should inherit from this class.

thedrow avatar Jul 23 '19 07:07 thedrow

Thanks for the report. Do you mean that someting like isinstance(my_coro_mock(), collections.abc.Coroutine) should be True?

It seems that Python itself doesn't enforce this with decorated functions:

import collections.abc, asyncio
@asyncio.coroutine
def foo():
  pass
 
isinstance(foo, collections.abc.Coroutine) # False
isinstance(foo(), collections.abc.Coroutine) # False

I mean, it makes sense for the return value of a CoroutineMock to inherit from abc.Coroutine, but it's not (yet) the case because @asyncio.coroutine doesn't make it so.

With the deprecation of old-style coroutines, we should have an opportunity to fix this.

Martiusweb avatar Jul 23 '19 08:07 Martiusweb

The doc say:

Note In CPython, generator-based coroutines (generators decorated with types.coroutine() or asyncio.coroutine()) are awaitables, even though they do not have an await() method. Using isinstance(gencoro, Coroutine) for them will return False. Use inspect.isawaitable() to detect them.

Kentzo avatar Nov 04 '19 18:11 Kentzo