gh-123087: `Lib/test/test_unittest/testmock/testasync.py: Replace usage of the deprecated asyncio.iscoroutinefunction with the inspect.iscoroutinefunction` (#123088)

asyncio.iscoroutinefunction -> inspect.iscoroutinefunction
This commit is contained in:
Kirill Podoprigora 2024-08-17 11:03:51 +03:00 committed by GitHub
parent 35d8ac7cd7
commit e6d5ff55d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,7 +8,7 @@
support.requires_working_socket(module=True) support.requires_working_socket(module=True)
from asyncio import run, iscoroutinefunction from asyncio import run
from unittest import IsolatedAsyncioTestCase from unittest import IsolatedAsyncioTestCase
from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock, Mock, from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock, Mock,
create_autospec, sentinel, _CallList, seal) create_autospec, sentinel, _CallList, seal)
@ -60,7 +60,7 @@ class AsyncPatchDecoratorTest(unittest.TestCase):
def test_is_coroutine_function_patch(self): def test_is_coroutine_function_patch(self):
@patch.object(AsyncClass, 'async_method') @patch.object(AsyncClass, 'async_method')
def test_async(mock_method): def test_async(mock_method):
self.assertTrue(iscoroutinefunction(mock_method)) self.assertTrue(inspect.iscoroutinefunction(mock_method))
test_async() test_async()
def test_is_async_patch(self): def test_is_async_patch(self):
@ -121,7 +121,7 @@ class AsyncPatchCMTest(unittest.TestCase):
def test_is_async_function_cm(self): def test_is_async_function_cm(self):
def test_async(): def test_async():
with patch.object(AsyncClass, 'async_method') as mock_method: with patch.object(AsyncClass, 'async_method') as mock_method:
self.assertTrue(iscoroutinefunction(mock_method)) self.assertTrue(inspect.iscoroutinefunction(mock_method))
test_async() test_async()
@ -155,7 +155,7 @@ def test_patch_dict_async_def(self):
async def test_async(): async def test_async():
self.assertEqual(foo['a'], 'b') self.assertEqual(foo['a'], 'b')
self.assertTrue(iscoroutinefunction(test_async)) self.assertTrue(inspect.iscoroutinefunction(test_async))
run(test_async()) run(test_async())
def test_patch_dict_async_def_context(self): def test_patch_dict_async_def_context(self):
@ -170,12 +170,11 @@ async def test_async():
class AsyncMockTest(unittest.TestCase): class AsyncMockTest(unittest.TestCase):
def test_iscoroutinefunction_default(self): def test_iscoroutinefunction_default(self):
mock = AsyncMock() mock = AsyncMock()
self.assertTrue(iscoroutinefunction(mock)) self.assertTrue(inspect.iscoroutinefunction(mock))
def test_iscoroutinefunction_function(self): def test_iscoroutinefunction_function(self):
async def foo(): pass async def foo(): pass
mock = AsyncMock(foo) mock = AsyncMock(foo)
self.assertTrue(iscoroutinefunction(mock))
self.assertTrue(inspect.iscoroutinefunction(mock)) self.assertTrue(inspect.iscoroutinefunction(mock))
def test_isawaitable(self): def test_isawaitable(self):
@ -188,7 +187,6 @@ def test_isawaitable(self):
def test_iscoroutinefunction_normal_function(self): def test_iscoroutinefunction_normal_function(self):
def foo(): pass def foo(): pass
mock = AsyncMock(foo) mock = AsyncMock(foo)
self.assertTrue(iscoroutinefunction(mock))
self.assertTrue(inspect.iscoroutinefunction(mock)) self.assertTrue(inspect.iscoroutinefunction(mock))
def test_future_isfuture(self): def test_future_isfuture(self):
@ -231,7 +229,6 @@ async def main():
run(main()) run(main())
self.assertTrue(iscoroutinefunction(spec))
self.assertTrue(inspect.iscoroutinefunction(spec)) self.assertTrue(inspect.iscoroutinefunction(spec))
self.assertTrue(asyncio.iscoroutine(awaitable)) self.assertTrue(asyncio.iscoroutine(awaitable))
self.assertTrue(inspect.iscoroutine(awaitable)) self.assertTrue(inspect.iscoroutine(awaitable))
@ -273,7 +270,6 @@ async def test_async():
awaitable = mock_method(1, 2, c=3) awaitable = mock_method(1, 2, c=3)
self.assertIsInstance(mock_method.mock, AsyncMock) self.assertIsInstance(mock_method.mock, AsyncMock)
self.assertTrue(iscoroutinefunction(mock_method))
self.assertTrue(inspect.iscoroutinefunction(mock_method)) self.assertTrue(inspect.iscoroutinefunction(mock_method))
self.assertTrue(asyncio.iscoroutine(awaitable)) self.assertTrue(asyncio.iscoroutine(awaitable))
self.assertTrue(inspect.iscoroutine(awaitable)) self.assertTrue(inspect.iscoroutine(awaitable))
@ -430,13 +426,13 @@ def test_async(async_method):
def test_is_async_AsyncMock(self): def test_is_async_AsyncMock(self):
mock = AsyncMock(spec_set=AsyncClass.async_method) mock = AsyncMock(spec_set=AsyncClass.async_method)
self.assertTrue(iscoroutinefunction(mock)) self.assertTrue(inspect.iscoroutinefunction(mock))
self.assertIsInstance(mock, AsyncMock) self.assertIsInstance(mock, AsyncMock)
def test_is_child_AsyncMock(self): def test_is_child_AsyncMock(self):
mock = MagicMock(spec_set=AsyncClass) mock = MagicMock(spec_set=AsyncClass)
self.assertTrue(iscoroutinefunction(mock.async_method)) self.assertTrue(inspect.iscoroutinefunction(mock.async_method))
self.assertFalse(iscoroutinefunction(mock.normal_method)) self.assertFalse(inspect.iscoroutinefunction(mock.normal_method))
self.assertIsInstance(mock.async_method, AsyncMock) self.assertIsInstance(mock.async_method, AsyncMock)
self.assertIsInstance(mock.normal_method, MagicMock) self.assertIsInstance(mock.normal_method, MagicMock)
self.assertIsInstance(mock, MagicMock) self.assertIsInstance(mock, MagicMock)
@ -606,8 +602,8 @@ def test_magic_methods_are_async_functions(self):
self.assertIsInstance(m_mock.__aenter__, AsyncMock) self.assertIsInstance(m_mock.__aenter__, AsyncMock)
self.assertIsInstance(m_mock.__aexit__, AsyncMock) self.assertIsInstance(m_mock.__aexit__, AsyncMock)
# AsyncMocks are also coroutine functions # AsyncMocks are also coroutine functions
self.assertTrue(iscoroutinefunction(m_mock.__aenter__)) self.assertTrue(inspect.iscoroutinefunction(m_mock.__aenter__))
self.assertTrue(iscoroutinefunction(m_mock.__aexit__)) self.assertTrue(inspect.iscoroutinefunction(m_mock.__aexit__))
class AsyncContextManagerTest(unittest.TestCase): class AsyncContextManagerTest(unittest.TestCase):
@ -746,11 +742,11 @@ def inner_test(mock_type):
mock_instance = mock_type(instance) mock_instance = mock_type(instance)
# Check that the mock and the real thing bahave the same # Check that the mock and the real thing bahave the same
# __aiter__ is not actually async, so not a coroutinefunction # __aiter__ is not actually async, so not a coroutinefunction
self.assertFalse(iscoroutinefunction(instance.__aiter__)) self.assertFalse(inspect.iscoroutinefunction(instance.__aiter__))
self.assertFalse(iscoroutinefunction(mock_instance.__aiter__)) self.assertFalse(inspect.iscoroutinefunction(mock_instance.__aiter__))
# __anext__ is async # __anext__ is async
self.assertTrue(iscoroutinefunction(instance.__anext__)) self.assertTrue(inspect.iscoroutinefunction(instance.__anext__))
self.assertTrue(iscoroutinefunction(mock_instance.__anext__)) self.assertTrue(inspect.iscoroutinefunction(mock_instance.__anext__))
for mock_type in [AsyncMock, MagicMock]: for mock_type in [AsyncMock, MagicMock]:
with self.subTest(f"test aiter and anext corourtine with {mock_type}"): with self.subTest(f"test aiter and anext corourtine with {mock_type}"):
@ -806,7 +802,7 @@ def test_assert_called_but_not_awaited(self):
mock = AsyncMock(AsyncClass) mock = AsyncMock(AsyncClass)
with assertNeverAwaited(self): with assertNeverAwaited(self):
mock.async_method() mock.async_method()
self.assertTrue(iscoroutinefunction(mock.async_method)) self.assertTrue(inspect.iscoroutinefunction(mock.async_method))
mock.async_method.assert_called() mock.async_method.assert_called()
mock.async_method.assert_called_once() mock.async_method.assert_called_once()
mock.async_method.assert_called_once_with() mock.async_method.assert_called_once_with()