diff --git a/Lib/test/test_unittest/testmock/testasync.py b/Lib/test/test_unittest/testmock/testasync.py index f96aef7325ff4c1..2f79f24c4bc7a30 100644 --- a/Lib/test/test_unittest/testmock/testasync.py +++ b/Lib/test/test_unittest/testmock/testasync.py @@ -222,6 +222,13 @@ def test_create_autospec(self): async def main(): await awaitable + self.assertTrue(spec.called) + self.assertEqual(spec.call_count, 1) + self.assertEqual(spec.call_args, call(1, 2, c=3)) + self.assertEqual(spec.call_args_list, [call(1, 2, c=3)]) + self.assertEqual(spec.mock_calls, [call(1, 2, c=3)]) + spec.assert_called_once_with(1, 2, c=3) + self.assertEqual(spec.await_count, 0) self.assertIsNone(spec.await_args) self.assertEqual(spec.await_args_list, []) @@ -232,6 +239,7 @@ async def main(): self.assertTrue(inspect.iscoroutinefunction(spec)) self.assertTrue(asyncio.iscoroutine(awaitable)) self.assertTrue(inspect.iscoroutine(awaitable)) + self.assertEqual(spec.call_count, 1) self.assertEqual(spec.await_count, 1) self.assertEqual(spec.await_args, call(1, 2, c=3)) self.assertEqual(spec.await_args_list, [call(1, 2, c=3)]) @@ -245,22 +253,17 @@ async def main(): def test_autospec_checks_signature(self): spec = create_autospec(async_func_args) - # signature is not checked when called - awaitable = spec() - self.assertListEqual(spec.mock_calls, []) - - async def main(): - await awaitable - - # but it is checked when awaited with self.assertRaises(TypeError): - run(main()) + spec() # _checksig_ raises before running or awaiting the mock + self.assertFalse(spec.called) + self.assertEqual(spec.call_count, 0) self.assertListEqual(spec.mock_calls, []) self.assertEqual(spec.await_count, 0) self.assertIsNone(spec.await_args) self.assertEqual(spec.await_args_list, []) + spec.assert_not_called() spec.assert_not_awaited() def test_patch_with_autospec(self): @@ -275,6 +278,11 @@ async def test_async(): self.assertTrue(inspect.iscoroutine(awaitable)) self.assertTrue(inspect.isawaitable(awaitable)) + # Call is recorded immediately, before awaiting + self.assertTrue(mock_method.called) + self.assertEqual(mock_method.call_count, 1) + mock_method.assert_called_once_with(1, 2, c=3) + # Verify the default values during mock setup self.assertEqual(mock_method.await_count, 0) self.assertEqual(mock_method.await_args_list, []) @@ -283,6 +291,7 @@ async def test_async(): await awaitable + self.assertEqual(mock_method.call_count, 1) self.assertEqual(mock_method.await_count, 1) self.assertEqual(mock_method.await_args, call(1, 2, c=3)) self.assertEqual(mock_method.await_args_list, [call(1, 2, c=3)]) @@ -292,12 +301,50 @@ async def test_async(): mock_method.assert_awaited() mock_method.reset_mock() + self.assertFalse(mock_method.called) + self.assertEqual(mock_method.call_count, 0) self.assertEqual(mock_method.await_count, 0) self.assertIsNone(mock_method.await_args) self.assertEqual(mock_method.await_args_list, []) run(test_async()) + def test_patch_with_autospec_side_effect_records_call_before_await(self): + async def async_side_effect(a, b, *, c): + return a + b + c + + async def test_async(): + with patch( + f"{__name__}.async_func_args", + side_effect=async_side_effect, + autospec=True, + ) as mock_method: + coro = async_func_args(1, 2, c=3) + self.assertTrue(mock_method.called) + mock_method.assert_called_once_with(1, 2, c=3) + mock_method.assert_not_awaited() + + result = await coro + self.assertEqual(result, 6) + mock_method.assert_awaited_once_with(1, 2, c=3) + + with patch.object( + AsyncClass, "async_method", side_effect=AsyncClass.async_method, + autospec=True, + ) as mock_method: + inst = AsyncClass() + self.assertTrue(inspect.iscoroutinefunction(inst.async_method)) + coro = inst.async_method() + self.assertTrue(mock_method.called) + mock_method.assert_called_once_with(inst) + mock_method.assert_not_awaited() + await coro + mock_method.assert_awaited_once_with(inst) + with self.assertRaises(TypeError): + inst.async_method("unexpected") + + run(test_async()) + class AsyncSpecTest(unittest.TestCase): def test_spec_normal_methods_on_class(self): diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index d3d47ff81e85969..93ae966b286f6e1 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -208,24 +208,15 @@ def checksig(*args, **kwargs): return funcopy def _set_async_signature(mock, original, instance=False, is_async_mock=False): - # creates an async function with signature (*args, **kwargs) that delegates to a - # mock. It still does signature checking by calling a lambda with the same - # signature as the original. - - skipfirst = isinstance(original, type) - func, sig = _get_signature_object(original, instance, skipfirst) - def checksig(*args, **kwargs): - sig.bind(*args, **kwargs) - _copy_func_details(func, checksig) - - name = original.__name__ - context = {'_checksig_': checksig, 'mock': mock} - src = """async def %s(*args, **kwargs): - _checksig_(*args, **kwargs) - return await mock(*args, **kwargs)""" % name - exec (src, context) - funcopy = context[name] - _setup_func(funcopy, mock, sig) + # creates a function marked as a coroutine function with signature + # (*args, **kwargs) that delegates to an async mock. Using a synchronous + # wrapper marked via markcoroutinefunction ensures signature checking and + # call recording happen immediately when called, rather than being + # deferred until the returned coroutine is awaited. + funcopy = _set_signature(mock, original, instance=instance) + if funcopy is mock: + return mock + inspect.markcoroutinefunction(funcopy) _setup_async_mock(funcopy) return funcopy diff --git a/Misc/NEWS.d/next/Library/2026-11-27-11-20-02.gh-issue-137594.aB3kQ9.rst b/Misc/NEWS.d/next/Library/2026-11-27-11-20-02.gh-issue-137594.aB3kQ9.rst new file mode 100644 index 000000000000000..c88eb8da95f9d1a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-27-11-20-02.gh-issue-137594.aB3kQ9.rst @@ -0,0 +1,3 @@ +Fix :func:`unittest.mock.create_autospec` for coroutine functions so that +signature validation and call recording occur immediately when called rather +than being deferred until the returned coroutine is awaited.