From 74a063a333e23ca8e3dfa638cdb366ed2d06b267 Mon Sep 17 00:00:00 2001 From: Matthew Kokotovich Date: Wed, 22 Jan 2020 08:33:59 -0600 Subject: [PATCH 1/3] bpo-39082: Allow AsyncMock to correcly patch static/class methods --- Lib/unittest/mock.py | 2 ++ Lib/unittest/test/testmock/testasync.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 5622917dc37439..8195a7bda08717 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -46,6 +46,8 @@ 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 73d31a2966882c..43b87498ef3735 100644 --- a/Lib/unittest/test/testmock/testasync.py +++ b/Lib/unittest/test/testmock/testasync.py @@ -19,6 +19,15 @@ async def async_method(self): 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 @@ def test_async(mock_method): 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) From ff16a46ee65b4c1cbb65ecde5f36be958f654733 Mon Sep 17 00:00:00 2001 From: Matthew Kokotovich Date: Fri, 24 Jan 2020 13:24:57 -0600 Subject: [PATCH 2/3] added NEWS entry --- .../NEWS.d/next/Library/2020-01-24-13-24-35.bpo-39082.qKgrq_.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-01-24-13-24-35.bpo-39082.qKgrq_.rst 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 index 00000000000000..829cfaa92b7249 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-01-24-13-24-35.bpo-39082.qKgrq_.rst @@ -0,0 +1 @@ +Allow AsyncMock to correcly patch static/class methods From 34af56dac68782aefd154be7010974a55d3783ac Mon Sep 17 00:00:00 2001 From: Matthew Kokotovich Date: Fri, 24 Jan 2020 15:11:46 -0600 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2020-01-24-13-24-35.bpo-39082.qKgrq_.rst --- .../next/Library/2020-01-24-13-24-35.bpo-39082.qKgrq_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 829cfaa92b7249..52c4ee1b33bdae 100644 --- 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 @@ -1 +1 @@ -Allow AsyncMock to correcly patch static/class methods +Allow AsyncMock to correctly patch static/class methods