Skip to content

Commit 25e115e

Browse files
authored
bpo-38161: Removes _AwaitEvent from AsyncMock. (GH-16443)
1 parent fb4ae15 commit 25e115e

File tree

3 files changed

+2
-39
lines changed

3 files changed

+2
-39
lines changed

Lib/unittest/mock.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ def _setup_async_mock(mock):
246246
mock.await_count = 0
247247
mock.await_args = None
248248
mock.await_args_list = _CallList()
249-
mock.awaited = _AwaitEvent(mock)
250249

251250
# Mock is not configured yet so the attributes are set
252251
# to a function and then the corresponding mock helper function
@@ -2116,7 +2115,6 @@ def __get__(self, obj, _type=None):
21162115

21172116

21182117
class AsyncMockMixin(Base):
2119-
awaited = _delegating_property('awaited')
21202118
await_count = _delegating_property('await_count')
21212119
await_args = _delegating_property('await_args')
21222120
await_args_list = _delegating_property('await_args_list')
@@ -2130,7 +2128,6 @@ def __init__(self, /, *args, **kwargs):
21302128
# It is set through __dict__ because when spec_set is True, this
21312129
# attribute is likely undefined.
21322130
self.__dict__['_is_coroutine'] = asyncio.coroutines._is_coroutine
2133-
self.__dict__['_mock_awaited'] = _AwaitEvent(self)
21342131
self.__dict__['_mock_await_count'] = 0
21352132
self.__dict__['_mock_await_args'] = None
21362133
self.__dict__['_mock_await_args_list'] = _CallList()
@@ -2159,7 +2156,6 @@ async def proxy():
21592156
self.await_count += 1
21602157
self.await_args = _call
21612158
self.await_args_list.append(_call)
2162-
await self.awaited._notify()
21632159

21642160
return await proxy()
21652161

@@ -2890,35 +2886,3 @@ async def __anext__(self):
28902886
except StopIteration:
28912887
pass
28922888
raise StopAsyncIteration
2893-
2894-
2895-
class _AwaitEvent:
2896-
def __init__(self, mock):
2897-
self._mock = mock
2898-
self._condition = None
2899-
2900-
async def _notify(self):
2901-
condition = self._get_condition()
2902-
try:
2903-
await condition.acquire()
2904-
condition.notify_all()
2905-
finally:
2906-
condition.release()
2907-
2908-
def _get_condition(self):
2909-
"""
2910-
Creation of condition is delayed, to minimize the chance of using the
2911-
wrong loop.
2912-
A user may create a mock with _AwaitEvent before selecting the
2913-
execution loop. Requiring a user to delay creation is error-prone and
2914-
inflexible. Instead, condition is created when user actually starts to
2915-
use the mock.
2916-
"""
2917-
# No synchronization is needed:
2918-
# - asyncio is thread unsafe
2919-
# - there are no awaits here, method will be executed without
2920-
# switching asyncio context.
2921-
if self._condition is None:
2922-
self._condition = asyncio.Condition()
2923-
2924-
return self._condition

Lib/unittest/test/testmock/testasync.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import unittest
55

66
from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock,
7-
create_autospec, _AwaitEvent, sentinel, _CallList)
7+
create_autospec, sentinel, _CallList)
88

99

1010
def tearDownModule():
@@ -178,7 +178,6 @@ async def main():
178178
self.assertEqual(spec.await_count, 0)
179179
self.assertIsNone(spec.await_args)
180180
self.assertEqual(spec.await_args_list, [])
181-
self.assertIsInstance(spec.awaited, _AwaitEvent)
182181
spec.assert_not_awaited()
183182

184183
asyncio.run(main())
@@ -212,7 +211,6 @@ async def test_async():
212211
self.assertEqual(mock_method.await_count, 0)
213212
self.assertEqual(mock_method.await_args_list, [])
214213
self.assertIsNone(mock_method.await_args)
215-
self.assertIsInstance(mock_method.awaited, _AwaitEvent)
216214
mock_method.assert_not_awaited()
217215

218216
await awaitable
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Removes _AwaitEvent from AsyncMock.

0 commit comments

Comments
 (0)