8000 Fixes RunTime warnings and missing io import. · lisroach/cpython@81ad0d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81ad0d1

Browse files
committed
Fixes RunTime warnings and missing io import.
1 parent f9bee6e commit 81ad0d1

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

Lib/unittest/mock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
__version__ = '1.0'
2727

2828
import asyncio
29+
import io
2930
import inspect
3031
import pprint
3132
import sys
@@ -2024,7 +2025,7 @@ async def _mock_call(_mock_self, *args, **kwargs):
20242025
side_effect = self.side_effect
20252026
if side_effect is not None and not callable(side_effect):
20262027
raise
2027-
return _raise(e)
2028+
return await _raise(e)
20282029

20292030
_call = self.call_args
20302031

Lib/unittest/test/testmock/testasync.py

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ def a(self):
2828

2929

3030
class AsyncPatchDecoratorTest(unittest.TestCase):
31+
def setUp(self):
32+
# Prevents altering the execution environment
33+
self.old_policy = asyncio.events._event_loop_policy
34+
35+
def tearDown(self):
36+
# Restore the original event loop policy.
37+
asyncio.events._event_loop_policy = self.old_policy
38+
3139
def test_is_coroutine_function_patch(self):
3240
@patch.object(AsyncClass, 'async_method')
3341
def test_async(mock_method):
@@ -37,11 +45,15 @@ def test_async(mock_method):
3745
def test_is_async_patch(self):
3846
@patch.object(AsyncClass, 'async_method')
3947
def test_async(mock_method):
40-
self.assertTrue(inspect.isawaitable(mock_method()))
48+
m = mock_method()
49+
self.assertTrue(inspect.isawaitable(m))
50+
asyncio.run(m)
4151

4252
@patch(f'{async_foo_name}.async_method')
4353
def test_no_parent_attribute(mock_method):
44-
self.assertTrue(inspect.isawaitable(mock_method()))
54+
m = mock_method()
55+
self.assertTrue(inspect.isawaitable(m))
56+
asyncio.run(m)
4557

4658
test_async()
4759
test_no_parent_attribute()
@@ -55,6 +67,13 @@ def test_async(mock_method):
5567

5668

5769
class AsyncPatchCMTest(unittest.TestCase):
70+
def setUp(self):
71+
self.old_policy = asyncio.events._event_loop_policy
72+
73+
def tearDown(self):
74+
# Restore the original event loop policy.
75+
asyncio.events._event_loop_policy = self.old_policy
76+
5877
def test_is_async_function_cm(self):
5978
def test_async():
6079
with patch.object(AsyncClass, 'async_method') as mock_method:
@@ -65,7 +84,9 @@ def test_async():
6584
def test_is_async_cm(self):
6685
def test_async():
6786
with patch.object(AsyncClass, 'async_method') as mock_method:
68-
self.assertTrue(inspect.isawaitable(mock_method()))
87+
m = mock_method()
88+
self.assertTrue(inspect.isawaitable(m))
89+
asyncio.run(m)
6990

7091
test_async()
7192

@@ -78,6 +99,13 @@ def test_async():
7899

79100

80101
class AsyncMockTest(unittest.TestCase):
102+
def setUp(self):
103+
self.old_policy = asyncio.events._event_loop_policy
104+
105+
def tearDown(self):
106+
# Restore the original event loop policy.
107+
asyncio.events._event_loop_policy = self.old_policy
108+
81109
def test_iscoroutinefunction_default(self):
82110
mock = AsyncMock()
83111
self.assertTrue(asyncio.iscoroutinefunction(mock))
@@ -90,7 +118,9 @@ async def foo(): pass
90118

91119
def test_isawaitable(self):
92120
mock = AsyncMock()
93-
self.assertTrue(inspect.isawaitable(mock()))
121+
m = mock()
122+
self.assertTrue(inspect.isawaitable(m))
123+
asyncio.run(m)
94124
self.assertIn('assert_awaited', dir(mock))
95125

96126
def test_iscoroutinefunction_normal_function(self):
@@ -120,35 +150,53 @@ def test_normal_method(mock_method):
120150

121151

122152
class AsyncSpecTest(unittest.TestCase):
153+
def setUp(self):
154+
self.old_policy = asyncio.events._event_loop_policy
155+
156+
def tearDown(self):
157+
# Restore the original event loop policy.
158+
asyncio.events._event_loop_policy = self.old_policy
123159
def test_spec_as_async_positional_magicmock(self):
124160
mock = MagicMock(async_func)
125161
self.assertIsInstance(mock, MagicMock)
126-
self.assertTrue(inspect.isawaitable(mock()))
162+
m = mock()
163+
self.assertTrue(inspect.isawaitable(m))
164+
asyncio.run(m)
127165

128166
def test_spec_as_async_kw_magicmock(self):
129167
mock = MagicMock(spec=async_func)
130168
self.assertIsInstance(mock, MagicMock)
131-
self.assertTrue(inspect.isawaitable(mock()))
169+
m =< 10822 /span> mock()
170+
self.assertTrue(inspect.isawaitable(m))
171+
asyncio.run(m)
132172

133173
def test_spec_as_async_kw_AsyncMock(self):
134174
mock = AsyncMock(spec=async_func)
135175
self.assertIsInstance(mock, AsyncMock)
136-
self.assertTrue(inspect.isawaitable(mock()))
176+
m = mock()
177+
self.assertTrue(inspect.isawaitable(m))
178+
asyncio.run(m)
137179

138180
def test_spec_as_async_positional_AsyncMock(self):
139181
mock = AsyncMock(async_func)
140182
self.assertIsInstance(mock, AsyncMock)
141-
self.assertTrue(inspect.isawaitable(mock()))
183+
m = mock()
184+
self.assertTrue(inspect.isawaitable(m))
185+
asyncio.run(m)
142186

143187
def test_spec_as_normal_kw_AsyncMock(self):
144188
mock = AsyncMock(spec=normal_func)
145189
self.assertIsInstance(mock, AsyncMock)
146-
self.assertTrue(inspect.isawaitable(mock()))
190+
m = mock()
191+
self.assertTrue(inspect.isawaitable(m))
192+
asyncio.run(m)
147193

148194
def test_spec_as_normal_positional_AsyncMock(self):
149195
mock = AsyncMock(normal_func)
150196
self.assertIsInstance(mock, AsyncMock)
151-
self.assertTrue(inspect.isawaitable(mock()))
197+
m = mock()
198+
self.assertTrue(inspect.isawaitable(m))
199+
asyncio.run(m)
152200

153201
def test_spec_async_mock(self):
154202
@patch.object(AsyncClass, 'async_method', spec=True)

0 commit comments

Comments
 (0)
0