diff options
author | Thomas Grainger <tagrain@gmail.com> | 2023-06-09 14:29:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-09 13:29:09 +0000 |
commit | 9bf8d825a66ea2a76169b917c12c237a6af2ed75 (patch) | |
tree | 03c736e98430b87b7c58bc76c67ee36d1e681046 /Lib/test/test_unittest/testmock/testasync.py | |
parent | 0f885ffa94aa9b69ff556e119cb17deb23a5a4b3 (diff) | |
download | cpython-9bf8d825a66ea2a76169b917c12c237a6af2ed75.tar.gz cpython-9bf8d825a66ea2a76169b917c12c237a6af2ed75.zip |
gh-94924: support `inspect.iscoroutinefunction` in `create_autospec(async_def)` (#94962)
* support inspect.iscoroutinefunction in create_autospec(async_def)
* test create_autospec with inspect.iscoroutine and inspect.iscoroutinefunction
* test when create_autospec functions check their signature
Diffstat (limited to 'Lib/test/test_unittest/testmock/testasync.py')
-rw-r--r-- | Lib/test/test_unittest/testmock/testasync.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_unittest/testmock/testasync.py b/Lib/test/test_unittest/testmock/testasync.py index 5f12f9f9566..e9e1f63e84d 100644 --- a/Lib/test/test_unittest/testmock/testasync.py +++ b/Lib/test/test_unittest/testmock/testasync.py @@ -232,7 +232,9 @@ class AsyncAutospecTest(unittest.TestCase): run(main()) self.assertTrue(iscoroutinefunction(spec)) + self.assertTrue(inspect.iscoroutinefunction(spec)) self.assertTrue(asyncio.iscoroutine(awaitable)) + self.assertTrue(inspect.iscoroutine(awaitable)) 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)]) @@ -244,6 +246,25 @@ class AsyncAutospecTest(unittest.TestCase): with self.assertRaises(AssertionError): spec.assert_any_await(e=1) + 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()) + + # _checksig_ raises before running or awaiting the mock + 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_awaited() def test_patch_with_autospec(self): @@ -253,7 +274,9 @@ class AsyncAutospecTest(unittest.TestCase): 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)) self.assertTrue(inspect.isawaitable(awaitable)) # Verify the default values during mock setup |