From 23b09362703579332c8ac001a980d34343e751e6 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Mon, 9 May 2022 14:12:16 +0200 Subject: [PATCH] Suppress most warnings --- MANIFEST.in | 2 +- nbclient/tests/test_client.py | 45 ++++++++++++++++++++++------------- nbclient/tests/test_util.py | 12 ++++++---- nbclient/util.py | 14 +++++------ pytest.ini | 2 ++ 5 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 pytest.ini diff --git a/MANIFEST.in b/MANIFEST.in index ea51c023..d5da1e73 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -3,7 +3,7 @@ include MANIFEST.in include requirements.txt include requirements-dev.txt include *.md -include tox.ini +include pytest.ini include pyproject.toml include .pre-commit-config.yaml include nbclient/py.typed diff --git a/nbclient/tests/test_client.py b/nbclient/tests/test_client.py index b4dbafa8..18b54ccd 100644 --- a/nbclient/tests/test_client.py +++ b/nbclient/tests/test_client.py @@ -9,6 +9,7 @@ import warnings from base64 import b64decode, b64encode from queue import Empty +from typing import Any from unittest.mock import MagicMock, Mock import nbformat @@ -78,11 +79,15 @@ class AsyncMock(Mock): pass -def make_async(mock_value): - async def _(): - return mock_value - - return _() +def make_future(obj: Any) -> asyncio.Future: + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + future: asyncio.Future = asyncio.Future(loop=loop) + future.set_result(obj) + return future def normalize_base64(b64_text): @@ -169,7 +174,7 @@ def shell_channel_message_mock(): # Return the message generator for # self.kc.shell_channel.get_msg => {'parent_header': {'msg_id': parent_id}} return AsyncMock( - return_value=make_async( + return_value=make_future( NBClientTestsBase.merge_dicts( { 'parent_header': {'msg_id': parent_id}, @@ -186,7 +191,7 @@ def iopub_messages_mock(): return AsyncMock( side_effect=[ # Default the parent_header so mocks don't need to include this - make_async( + make_future( NBClientTestsBase.merge_dicts({'parent_header': {'msg_id': parent_id}}, msg) ) for msg in messages @@ -215,7 +220,7 @@ def test_mock_wrapper(self): iopub_channel=MagicMock(get_msg=message_mock), shell_channel=MagicMock(get_msg=shell_channel_message_mock()), execute=MagicMock(return_value=parent_id), - is_alive=MagicMock(return_value=make_async(True)), + is_alive=MagicMock(return_value=make_future(True)), ) executor.parent_id = parent_id return func(self, executor, cell_mock, message_mock) @@ -387,11 +392,15 @@ def test_async_parallel_notebooks(capfd, tmpdir): res = notebook_resources() with modified_env({"NBEXECUTE_TEST_PARALLEL_TMPDIR": str(tmpdir)}): - tasks = [ - async_run_notebook(input_file.format(label=label), opts, res) for label in ("A", "B") - ] - loop = asyncio.get_event_loop() - loop.run_until_complete(asyncio.gather(*tasks)) + + async def run_tasks(): + tasks = [ + async_run_notebook(input_file.format(label=label), opts, res) + for label in ("A", "B") + ] + await asyncio.gather(*tasks) + + asyncio.run(run_tasks()) captured = capfd.readouterr() assert filter_messages_on_error_output(captured.err) == "" @@ -412,9 +421,11 @@ def test_many_async_parallel_notebooks(capfd): # run once, to trigger creating the original context run_notebook(input_file, opts, res) - tasks = [async_run_notebook(input_file, opts, res) for i in range(4)] - loop = asyncio.get_event_loop() - loop.run_until_complete(asyncio.gather(*tasks)) + async def run_tasks(): + tasks = [async_run_notebook(input_file, opts, res) for i in range(4)] + await asyncio.gather(*tasks) + + asyncio.run(run_tasks()) captured = capfd.readouterr() assert filter_messages_on_error_output(captured.err) == "" @@ -966,7 +977,7 @@ def message_seq(messages): message_mock.side_effect = message_seq(list(message_mock.side_effect)[:-1]) executor.kc.shell_channel.get_msg = Mock( - return_value=make_async({'parent_header': {'msg_id': executor.parent_id}}) + return_value=make_future({'parent_header': {'msg_id': executor.parent_id}}) ) executor.raise_on_iopub_timeout = True diff --git a/nbclient/tests/test_util.py b/nbclient/tests/test_util.py index 91022579..dc206826 100644 --- a/nbclient/tests/test_util.py +++ b/nbclient/tests/test_util.py @@ -14,11 +14,15 @@ async def some_async_function(): def test_nested_asyncio_with_existing_ioloop(): - ioloop = asyncio.new_event_loop() - try: - asyncio.set_event_loop(ioloop) + async def _test(): assert some_async_function() == 42 - assert asyncio.get_event_loop() is ioloop + return asyncio.get_running_loop() + + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + try: + event_loop = loop.run_until_complete(_test()) + assert event_loop is loop finally: asyncio._set_running_loop(None) # it seems nest_asyncio doesn't reset this diff --git a/nbclient/util.py b/nbclient/util.py index 8000a04c..0a352c2d 100644 --- a/nbclient/util.py +++ b/nbclient/util.py @@ -39,16 +39,14 @@ def check_patch_tornado() -> None: def just_run(coro: Awaitable) -> Any: """Make the coroutine run, even if there is an event loop running (using nest_asyncio)""" - # original from vaex/asyncio.py - loop = asyncio._get_running_loop() + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = None if loop is None: had_running_loop = False - try: - loop = asyncio.get_event_loop() - except RuntimeError: - # we can still get 'There is no current event loop in ...' - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) else: had_running_loop = True if had_running_loop: diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..2f4c80e3 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +asyncio_mode = auto