Skip to content

[3.8] bpo-38161: Removes _AwaitEvent from AsyncMock. (GH-16443) #16481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2019
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
36 changes: 0 additions & 36 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ def _setup_async_mock(mock):
mock.await_count = 0
mock.await_args = None
mock.await_args_list = _CallList()
mock.awaited = _AwaitEvent(mock)

# Mock is not configured yet so the attributes are set
# to a function and then the corresponding mock helper function
Expand Down Expand Up @@ -2102,7 +2101,6 @@ def __get__(self, obj, _type=None):


class AsyncMockMixin(Base):
awaited = _delegating_property('awaited')
await_count = _delegating_property('await_count')
await_args = _delegating_property('await_args')
await_args_list = _delegating_property('await_args_list')
Expand All @@ -2116,7 +2114,6 @@ def __init__(self, /, *args, **kwargs):
# It is set through __dict__ because when spec_set is True, this
# attribute is likely undefined.
self.__dict__['_is_coroutine'] = asyncio.coroutines._is_coroutine
self.__dict__['_mock_awaited'] = _AwaitEvent(self)
self.__dict__['_mock_await_count'] = 0
self.__dict__['_mock_await_args'] = None
self.__dict__['_mock_await_args_list'] = _CallList()
Expand Down Expand Up @@ -2145,7 +2142,6 @@ async def proxy():
self.await_count += 1
self.await_args = _call
self.await_args_list.append(_call)
await self.awaited._notify()

return await proxy()

Expand Down Expand Up @@ -2878,35 +2874,3 @@ async def __anext__(self):
except StopIteration:
pass
raise StopAsyncIteration


class _AwaitEvent:
def __init__(self, mock):
self._mock = mock
self._condition = None

async def _notify(self):
condition = self._get_condition()
try:
await condition.acquire()
condition.notify_all()
finally:
condition.release()

def _get_condition(self):
"""
Creation of condition is delayed, to minimize the chance of using the
wrong loop.
A user may create a mock with _AwaitEvent before selecting the
execution loop. Requiring a user to delay creation is error-prone and
inflexible. Instead, condition is created when user actually starts to
use the mock.
"""
# No synchronization is needed:
# - asyncio is thread unsafe
# - there are no awaits here, method will be executed without
# switching asyncio context.
if self._condition is None:
self._condition = asyncio.Condition()

return self._condition
4 changes: 1 addition & 3 deletions Lib/unittest/test/testmock/testasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import unittest

from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock,
create_autospec, _AwaitEvent, sentinel, _CallList)
create_autospec, sentinel, _CallList)


def tearDownModule():
Expand Down Expand Up @@ -178,7 +178,6 @@ async def main():
self.assertEqual(spec.await_count, 0)
self.assertIsNone(spec.await_args)
self.assertEqual(spec.await_args_list, [])
self.assertIsInstance(spec.awaited, _AwaitEvent)
spec.assert_not_awaited()

asyncio.run(main())
Expand Down Expand Up @@ -208,7 +207,6 @@ async def test_async():
self.assertEqual(mock_method.await_count, 0)
self.assertEqual(mock_method.await_args_list, [])
self.assertIsNone(mock_method.await_args)
self.assertIsInstance(mock_method.awaited, _AwaitEvent)
mock_method.assert_not_awaited()

await awaitable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removes _AwaitEvent from AsyncMock.