]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-84753: Make inspect.iscoroutinefunction() work with AsyncMock (#94050)
authorMehdi ABAAKOUK <sileht@sileht.net>
Thu, 30 Jun 2022 17:08:38 +0000 (19:08 +0200)
committerGitHub <noreply@github.com>
Thu, 30 Jun 2022 17:08:38 +0000 (19:08 +0200)
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>
Lib/inspect.py
Lib/test/test_asyncio/test_tasks.py
Lib/test/test_inspect.py
Lib/unittest/mock.py
Misc/NEWS.d/next/Library/2022-06-21-11-40-31.gh-issue-84753.FW1pxO.rst [new file with mode: 0644]

index 6e744712f014b1ba3d83a664195333407959999d..cbc0632484b83264dcce14c833347bc6563a16fb 100644 (file)
@@ -395,7 +395,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)
 
index 69cecb3ab12070d86c916d7bd5b55674a40d4ac6..bde4defdf0129e97f637cbab4616e165dc465ac3 100644 (file)
@@ -1649,6 +1649,7 @@ class BaseTaskTests:
         self.assertTrue(asyncio.iscoroutinefunction(fn2))
 
         self.assertFalse(asyncio.iscoroutinefunction(mock.Mock()))
+        self.assertTrue(asyncio.iscoroutinefunction(mock.AsyncMock()))
 
     def test_coroutine_non_gen_function(self):
         async def func():
index ae1842704d37df1b0b1d83c8d05e288f6b0bf356..be9f29e04ae110bdbb97656154135dab58e6f496 100644 (file)
@@ -202,6 +202,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(
@@ -210,6 +214,10 @@ class TestPredicates(IsTestBase):
                     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(
@@ -218,6 +226,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(
index 8262b7170b76b4ba14da9ae1407a9cd5ad9d7726..cd46fea5162aba191548fe1d659d0ecce3aaa2db 100644 (file)
@@ -2175,6 +2175,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 (file)
index 0000000..f701d2a
--- /dev/null
@@ -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.