Skip to content

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

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
Aug 17, 2024
Merged
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
34 changes: 15 additions & 19 deletions Lib/test/test_unittest/testmock/testasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

support.requires_working_socket(module=True)

from asyncio import run, iscoroutinefunction
from asyncio import run
from unittest import IsolatedAsyncioTestCase
from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock, Mock,
create_autospec, sentinel, _CallList, seal)
Expand Down Expand Up @@ -60,7 +60,7 @@ class AsyncPatchDecoratorTest(unittest.TestCase):
def test_is_coroutine_function_patch(self):
@patch.object(AsyncClass, 'async_method')
def test_async(mock_method):
self.assertTrue(iscoroutinefunction(mock_method))
self.assertTrue(inspect.iscoroutinefunction(mock_method))
test_async()

def test_is_async_patch(self):
Expand Down Expand Up @@ -121,7 +121,7 @@ class AsyncPatchCMTest(unittest.TestCase):
def test_is_async_function_cm(self):
def test_async():
with patch.object(AsyncClass, 'async_method') as mock_method:
self.assertTrue(iscoroutinefunction(mock_method))
self.assertTrue(inspect.iscoroutinefunction(mock_method))

test_async()

Expand Down Expand Up @@ -155,7 +155,7 @@ def test_patch_dict_async_def(self):
async def test_async():
self.assertEqual(foo['a'], 'b')

self.assertTrue(iscoroutinefunction(test_async))
self.assertTrue(inspect.iscoroutinefunction(test_async))
run(test_async())

def test_patch_dict_async_def_context(self):
Expand All @@ -170,12 +170,11 @@ async def test_async():
class AsyncMockTest(unittest.TestCase):
def test_iscoroutinefunction_default(self):
mock = AsyncMock()
self.assertTrue(iscoroutinefunction(mock))
self.assertTrue(inspect.iscoroutinefunction(mock))

def test_iscoroutinefunction_function(self):
async def foo(): pass
mock = AsyncMock(foo)
self.assertTrue(iscoroutinefunction(mock))
self.assertTrue(inspect.iscoroutinefunction(mock))

def test_isawaitable(self):
Expand All @@ -188,7 +187,6 @@ def test_isawaitable(self):
def test_iscoroutinefunction_normal_function(self):
def foo(): pass
mock = AsyncMock(foo)
self.assertTrue(iscoroutinefunction(mock))
self.assertTrue(inspect.iscoroutinefunction(mock))

def test_future_isfuture(self):
Expand Down Expand Up @@ -231,7 +229,6 @@ async def main():

run(main())

self.assertTrue(iscoroutinefunction(spec))
self.assertTrue(inspect.iscoroutinefunction(spec))
self.assertTrue(asyncio.iscoroutine(awaitable))
self.assertTrue(inspect.iscoroutine(awaitable))
Expand Down Expand Up @@ -273,7 +270,6 @@ async def test_async():
awaitable = mock_method(1, 2, c=3)
self.assertIsInstance(mock_method.mock, AsyncMock)

self.assertTrue(iscoroutinefunction(mock_method))
self.assertTrue(inspect.iscoroutinefunction(mock_method))
self.assertTrue(asyncio.iscoroutine(awaitable))
self.assertTrue(inspect.iscoroutine(awaitable))
Expand Down Expand Up @@ -430,13 +426,13 @@ def test_async(async_method):

def test_is_async_AsyncMock(self):
mock = AsyncMock(spec_set=AsyncClass.async_method)
self.assertTrue(iscoroutinefunction(mock))
self.assertTrue(inspect.iscoroutinefunction(mock))
self.assertIsInstance(mock, AsyncMock)

def test_is_child_AsyncMock(self):
mock = MagicMock(spec_set=AsyncClass)
self.assertTrue(iscoroutinefunction(mock.async_method))
self.assertFalse(iscoroutinefunction(mock.normal_method))
self.assertTrue(inspect.iscoroutinefunction(mock.async_method))
self.assertFalse(inspect.iscoroutinefunction(mock.normal_method))
self.assertIsInstance(mock.async_method, AsyncMock)
self.assertIsInstance(mock.normal_method, MagicMock)
self.assertIsInstance(mock, MagicMock)
Expand Down Expand Up @@ -606,8 +602,8 @@ def test_magic_methods_are_async_functions(self):
self.assertIsInstance(m_mock.__aenter__, AsyncMock)
self.assertIsInstance(m_mock.__aexit__, AsyncMock)
# AsyncMocks are also coroutine functions
self.assertTrue(iscoroutinefunction(m_mock.__aenter__))
self.assertTrue(iscoroutinefunction(m_mock.__aexit__))
self.assertTrue(inspect.iscoroutinefunction(m_mock.__aenter__))
self.assertTrue(inspect.iscoroutinefunction(m_mock.__aexit__))

class AsyncContextManagerTest(unittest.TestCase):

Expand Down Expand Up @@ -746,11 +742,11 @@ def inner_test(mock_type):
mock_instance = mock_type(instance)
# Check that the mock and the real thing bahave the same
# __aiter__ is not actually async, so not a coroutinefunction
self.assertFalse(iscoroutinefunction(instance.__aiter__))
self.assertFalse(iscoroutinefunction(mock_instance.__aiter__))
self.assertFalse(inspect.iscoroutinefunction(instance.__aiter__))
self.assertFalse(inspect.iscoroutinefunction(mock_instance.__aiter__))
# __anext__ is async
self.assertTrue(iscoroutinefunction(instance.__anext__))
self.assertTrue(iscoroutinefunction(mock_instance.__anext__))
self.assertTrue(inspect.iscoroutinefunction(instance.__anext__))
self.assertTrue(inspect.iscoroutinefunction(mock_instance.__anext__))

for mock_type in [AsyncMock, MagicMock]:
with self.subTest(f"test aiter and anext corourtine with {mock_type}"):
Expand Down Expand Up @@ -806,7 +802,7 @@ def test_assert_called_but_not_awaited(self):
mock = AsyncMock(AsyncClass)
with assertNeverAwaited(self):
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_once()
mock.async_method.assert_called_once_with()
Expand Down
Loading