From cabfb5a84fe03cb4b6f527a52bcdfbfcebabc727 Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Tue, 10 Sep 2019 10:52:14 +0100 Subject: [PATCH 1/4] Removes __code__ check from _is_async_obj. --- Lib/unittest/mock.py | 5 ++--- Lib/unittest/test/testmock/testasync.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index c26b367a4dc290..50e49596551935 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -46,10 +46,9 @@ _safe_super = super def _is_async_obj(obj): - if getattr(obj, '__code__', None): - return asyncio.iscoroutinefunction(obj) or inspect.isawaitable(obj) - else: + if _is_instance_mock(obj) and not isinstance(obj, AsyncMock): return False + return asyncio.iscoroutinefunction(obj) or inspect.isawaitable(obj) def _is_async_func(func): diff --git a/Lib/unittest/test/testmock/testasync.py b/Lib/unittest/test/testmock/testasync.py index 865cfdc0026465..550234223cc372 100644 --- a/Lib/unittest/test/testmock/testasync.py +++ b/Lib/unittest/test/testmock/testasync.py @@ -18,6 +18,10 @@ async def async_method(self): def normal_method(self): pass +class AwaitableClass: + def __await__(self): + yield + async def async_func(): pass @@ -160,6 +164,10 @@ def test_create_autospec_instance(self): with self.assertRaises(RuntimeError): create_autospec(async_func, instance=True) + def test_create_autospec_awaitable_class(self): + awaitable_mock = create_autospec(spec=AwaitableClass()) + self.assertIsInstance(create_autospec(awaitable_mock), AsyncMock) + def test_create_autospec(self): spec = create_autospec(async_func_args) awaitable = spec(1, 2, c=3) @@ -321,6 +329,13 @@ def test_is_child_AsyncMock(self): self.assertIsInstance(mock.normal_method, MagicMock) self.assertIsInstance(mock, MagicMock) + def test_magicmock_lambda_spec(self): + mock_obj = MagicMock() + mock_obj.mock_func = MagicMock(spec=lambda x: x) + + with patch.object(mock_obj, "mock_func") as cm: + self.assertIsInstance(cm, MagicMock) + class AsyncArguments(unittest.TestCase): def test_add_return_value(self): From dccaaf61288999d4b5215d2d272e0ae3b984851e Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Tue, 10 Sep 2019 11:00:14 +0100 Subject: [PATCH 2/4] Adds news blurb. --- .../next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst diff --git a/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst b/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst new file mode 100644 index 00000000000000..78edd5c91a4b28 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst @@ -0,0 +1,3 @@ +Removes `__code__` check in AsyncMock's `_is_async_obj` that incorrectly +evaluated function specs as async objects but failed to evaluate classes +with `__await__` and but no `__code__` attribute defined as async objects. From 2099348f367759dcb7ccee00f57e19603f4bc2eb Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Tue, 10 Sep 2019 11:01:20 +0100 Subject: [PATCH 3/4] Fixes typo in news blurb. --- .../next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst b/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst index 78edd5c91a4b28..c16f1271266cc5 100644 --- a/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst +++ b/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst @@ -1,3 +1,3 @@ Removes `__code__` check in AsyncMock's `_is_async_obj` that incorrectly evaluated function specs as async objects but failed to evaluate classes -with `__await__` and but no `__code__` attribute defined as async objects. +with `__await__` but no `__code__` attribute defined as async objects. From dc9fc73a2a9843dfb9fc3b4b66999f438bb05a5a Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Tue, 10 Sep 2019 11:32:18 +0100 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst Co-Authored-By: Xtreak --- .../next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst b/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst index c16f1271266cc5..27fd1e46963c72 100644 --- a/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst +++ b/Misc/NEWS.d/next/Library/2019-09-10-10-59-50.bpo-37251.8zn2o3.rst @@ -1,3 +1,3 @@ -Removes `__code__` check in AsyncMock's `_is_async_obj` that incorrectly +Remove `__code__` check in AsyncMock that incorrectly evaluated function specs as async objects but failed to evaluate classes with `__await__` but no `__code__` attribute defined as async objects.