diff options
author | Matthew Kokotovich <mkokotovich@gmail.com> | 2020-01-25 10:17:47 (GMT) |
---|---|---|
committer | Chris Withers <chris@withers.org> | 2020-01-25 10:17:47 (GMT) |
commit | 62865f4532094017a9b780b704686ca9734bc329 (patch) | |
tree | b0673a2959839526c335b9c6bd37e5bdc0bfd68a /Lib | |
parent | d0d9fa8c5e30aff71b6d5e8b2673396622f33270 (diff) | |
download | cpython-62865f4532094017a9b780b704686ca9734bc329.zip cpython-62865f4532094017a9b780b704686ca9734bc329.tar.gz cpython-62865f4532094017a9b780b704686ca9734bc329.tar.bz2 |
bpo-39082: Allow AsyncMock to correctly patch static/class methods (GH-18116)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/unittest/mock.py | 2 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testasync.py | 23 |
2 files changed, 25 insertions, 0 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 92b596f..047ae7c 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -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) diff --git a/Lib/unittest/test/testmock/testasync.py b/Lib/unittest/test/testmock/testasync.py index 73d31a2..43b8749 100644 --- a/Lib/unittest/test/testmock/testasync.py +++ b/Lib/unittest/test/testmock/testasync.py @@ -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) |