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

[3.10] gh-84753: Make inspect.iscoroutinefunction() work with AsyncMock (GH-94050) #94461

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def _has_code_flag(f, flag):
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)

Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,7 @@ def fn2():
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)
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def test_iscoroutine(self):
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(
Expand All @@ -196,6 +200,10 @@ def test_iscoroutine(self):
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(
Expand All @@ -204,6 +212,12 @@ def test_iscoroutine(self):
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(
Expand Down
4 changes: 4 additions & 0 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,10 @@ def __init__(self, /, *args, **kwargs):
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
: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.