Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions 5 Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
15 changes: 15 additions & 0 deletions 15 Lib/unittest/test/testmock/testasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.