Skip to content

Commit 36e7e4a

Browse files
miss-islingtonlisroach
authored andcommitted
bpo-38161: Removes _AwaitEvent from AsyncMock. (GH-16443) (GH-16481)
1 parent b76ab35 commit 36e7e4a

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
@@ -2102,7 +2101,6 @@ def __get__(self, obj, _type=None):
21022101

21032102

21042103
class AsyncMockMixin(Base):
2105-
awaited = _delegating_property('awaited')
21062104
await_count = _delegating_property('await_count')
21072105
await_args = _delegating_property('await_args')
21082106
await_args_list = _delegating_property('await_args_list')
@@ -2116,7 +2114,6 @@ def __init__(self, /, *args, **kwargs):
21162114
# It is set through __dict__ because when spec_set is True, this
21172115
# attribute is likely undefined.
21182116
self.__dict__['_is_coroutine'] = asyncio.coroutines._is_coroutine
2119-
self.__dict__['_mock_awaited'] = _AwaitEvent(self)
21202117
self.__dict__['_mock_await_count'] = 0
21212118
self.__dict__['_mock_await_args'] = None
21222119
self.__dict__['_mock_await_args_list'] = _CallList()
@@ -2145,7 +2142,6 @@ async def proxy():
21452142
self.await_count += 1
21462143
self.await_args = _call
21472144
self.await_args_list.append(_call)
2148-
await self.awaited._notify()
21492145

21502146
return await proxy()
21512147

@@ -2878,35 +2874,3 @@ async def __anext__(self):
28782874
except StopIteration:
28792875
pass
28802876
raise StopAsyncIteration
2881-
2882-
2883-
class _AwaitEvent:
2884-
def __init__(self, mock):
2885-
self._mock = mock
2886-
self._condition = None
2887-
2888-
async def _notify(self):
2889-
condition = self._get_condition()
2890-
try:
2891-
await condition.acquire()
2892-
condition.notify_all()
2893-
finally:
2894-
condition.release()
2895-
2896-
def _get_condition(self):
2897-
"""
2898-
Creation of condition is delayed, to minimize the chance of using the
2899-
wrong loop.
2900-
A user may create a mock with _AwaitEvent before selecting the
2901-
execution loop. Requiring a user to delay creation is error-prone and
2902-
inflexible. Instead, condition is created when user actually starts to
2903-
use the mock.
2904-
"""
2905-
# No synchronization is needed:
2906-
# - asyncio is thread unsafe
2907-
# - there are no awaits here, method will be executed without
2908-
# switching asyncio context.
2909-
if self._condition is None:
2910-
self._condition = asyncio.Condition()
2911-
2912-
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())
@@ -208,7 +207,6 @@ async def test_async():
208207
self.assertEqual(mock_method.await_count, 0)
209208
self.assertEqual(mock_method.await_args_list, [])
210209
self.assertIsNone(mock_method.await_args)
211-
self.assertIsInstance(mock_method.awaited, _AwaitEvent)
212210
mock_method.assert_not_awaited()
213211

214212
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)