]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39082: Allow AsyncMock to correctly patch static/class methods (GH-18116)
authorMatthew Kokotovich <mkokotovich@gmail.com>
Sat, 25 Jan 2020 10:17:47 +0000 (04:17 -0600)
committerChris Withers <chris@withers.org>
Sat, 25 Jan 2020 10:17:47 +0000 (10:17 +0000)
Lib/unittest/mock.py
Lib/unittest/test/testmock/testasync.py
Misc/NEWS.d/next/Library/2020-01-24-13-24-35.bpo-39082.qKgrq_.rst [new file with mode: 0644]

index 92b596f672c28d3a94596b3205a5c113233f7ed0..047ae7c25599badb62ceae6bb75ed28d81d49b0c 100644 (file)
@@ -46,6 +46,8 @@ _safe_super = super
 def _is_async_obj(obj):
     if _is_instance_mock(obj) and not isinstance(obj, AsyncMock):
         return False
+    if hasattr(obj, '__func__'):
+        obj = getattr(obj, '__func__')
     return asyncio.iscoroutinefunction(obj) or inspect.isawaitable(obj)
 
 
index 73d31a2966882cce42a5761e9b72f4dabad5363d..43b87498ef3735b2beb9842e53d81c20a1cb3fbf 100644 (file)
@@ -19,6 +19,15 @@ class AsyncClass:
     def normal_method(self):
         pass
 
+    @classmethod
+    async def async_class_method(cls):
+        pass
+
+    @staticmethod
+    async def async_static_method():
+        pass
+
+
 class AwaitableClass:
     def __await__(self):
         yield
@@ -71,6 +80,20 @@ class AsyncPatchDecoratorTest(unittest.TestCase):
 
         test_async()
 
+    def test_is_AsyncMock_patch_staticmethod(self):
+        @patch.object(AsyncClass, 'async_static_method')
+        def test_async(mock_method):
+            self.assertIsInstance(mock_method, AsyncMock)
+
+        test_async()
+
+    def test_is_AsyncMock_patch_classmethod(self):
+        @patch.object(AsyncClass, 'async_class_method')
+        def test_async(mock_method):
+            self.assertIsInstance(mock_method, AsyncMock)
+
+        test_async()
+
     def test_async_def_patch(self):
         @patch(f"{__name__}.async_func", return_value=1)
         @patch(f"{__name__}.async_func_args", return_value=2)
diff --git a/Misc/NEWS.d/next/Library/2020-01-24-13-24-35.bpo-39082.qKgrq_.rst b/Misc/NEWS.d/next/Library/2020-01-24-13-24-35.bpo-39082.qKgrq_.rst
new file mode 100644 (file)
index 0000000..52c4ee1
--- /dev/null
@@ -0,0 +1 @@
+Allow AsyncMock to correctly patch static/class methods