The inspect version was not working with unittest.mock.AsyncMock.
The fix introduces special-casing of AsyncMock in
`inspect.iscoroutinefunction` equivalent to the one
performed in `asyncio.iscoroutinefunction`.
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
(cherry picked from commit
4261b6bffc0b8bb5c6d4d80578a81b7520f4aefc)
Co-authored-by: Mehdi ABAAKOUK <sileht@sileht.net>
while ismethod(f):
f = f.__func__
f = functools._unwrap_partial(f)
- if not isfunction(f):
+ if not (isfunction(f) or _signature_is_functionlike(f)):
return False
return bool(f.__code__.co_flags & flag)
self.assertTrue(asyncio.iscoroutinefunction(fn2))
self.assertFalse(asyncio.iscoroutinefunction(mock.Mock()))
+ self.assertTrue(asyncio.iscoroutinefunction(mock.AsyncMock()))
def test_yield_vs_yield_from(self):
fut = self.new_future(self.loop)
gen_coroutine_function_example))))
self.assertTrue(inspect.isgenerator(gen_coro))
+ self.assertFalse(
+ inspect.iscoroutinefunction(unittest.mock.Mock()))
+ self.assertTrue(
+ inspect.iscoroutinefunction(unittest.mock.AsyncMock()))
self.assertTrue(
inspect.iscoroutinefunction(coroutine_function_example))
self.assertTrue(
coroutine_function_example))))
self.assertTrue(inspect.iscoroutine(coro))
+ self.assertFalse(
+ inspect.isgeneratorfunction(unittest.mock.Mock()))
+ self.assertFalse(
+ inspect.isgeneratorfunction(unittest.mock.AsyncMock()))
self.assertFalse(
inspect.isgeneratorfunction(coroutine_function_example))
self.assertFalse(
coroutine_function_example))))
self.assertFalse(inspect.isgenerator(coro))
+ self.assertFalse(
+ inspect.isasyncgenfunction(unittest.mock.Mock()))
+ self.assertFalse(
+ inspect.isasyncgenfunction(unittest.mock.AsyncMock()))
+ self.assertFalse(
+ inspect.isasyncgenfunction(coroutine_function_example))
self.assertTrue(
inspect.isasyncgenfunction(async_generator_function_example))
self.assertTrue(
code_mock = NonCallableMock(spec_set=CodeType)
code_mock.co_flags = inspect.CO_COROUTINE
self.__dict__['__code__'] = code_mock
+ self.__dict__['__name__'] = 'AsyncMock'
+ self.__dict__['__defaults__'] = tuple()
+ self.__dict__['__kwdefaults__'] = {}
+ self.__dict__['__annotations__'] = None
async def _execute_mock_call(self, /, *args, **kwargs):
# This is nearly just like super(), except for special handling
--- /dev/null
+:func:`inspect.iscoroutinefunction` now properly returns ``True`` when an instance
+of :class:`unittest.mock.AsyncMock` is passed to it. This makes it consistent with
+behavior of :func:`asyncio.iscoroutinefunction`. Patch by Mehdi ABAAKOUK.