summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-06-30 11:02:15 -0700
committerGitHub <noreply@github.com>2022-06-30 20:02:15 +0200
commit47f23b2d8a14a2f182375c840776eadfc371c726 (patch)
tree76ead06fbb8909c527a9438d7069c795b71ce87f
parentd915ed297804041c822a595c0f44e52a4c385302 (diff)
downloadcpython-git-47f23b2d8a14a2f182375c840776eadfc371c726.tar.gz
gh-84753: Make inspect.iscoroutinefunction() work with AsyncMock (GH-94050) (GH-94461)
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>
-rw-r--r--Lib/inspect.py2
-rw-r--r--Lib/test/test_asyncio/test_tasks.py1
-rw-r--r--Lib/test/test_inspect.py14
-rw-r--r--Lib/unittest/mock.py4
-rw-r--r--Misc/NEWS.d/next/Library/2022-06-21-11-40-31.gh-issue-84753.FW1pxO.rst3
5 files changed, 23 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index c5881cc808..01df575e4b 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -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)
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 0007b44a22..05a822ba45 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -1723,6 +1723,7 @@ class BaseTaskTests:
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)
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 28e4f5b4a7..eaefe946e8 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -188,6 +188,10 @@ class TestPredicates(IsTestBase):
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(
@@ -197,6 +201,10 @@ class TestPredicates(IsTestBase):
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(
inspect.isgeneratorfunction(
@@ -204,6 +212,12 @@ class TestPredicates(IsTestBase):
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(
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 7152f86ed9..3f7185b967 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2186,6 +2186,10 @@ class AsyncMockMixin(Base):
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
diff --git a/Misc/NEWS.d/next/Library/2022-06-21-11-40-31.gh-issue-84753.FW1pxO.rst b/Misc/NEWS.d/next/Library/2022-06-21-11-40-31.gh-issue-84753.FW1pxO.rst
new file mode 100644
index 0000000000..f701d2a1af
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-06-21-11-40-31.gh-issue-84753.FW1pxO.rst
@@ -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.