Skip to content

Commit c77a735

Browse files
Suppress most warnings (#232)
1 parent 6bdd51a commit c77a735

File tree

5 files changed

+45
-30
lines changed

5 files changed

+45
-30
lines changed

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include MANIFEST.in
33
include requirements.txt
44
include requirements-dev.txt
55
include *.md
6-
include tox.ini
6+
include pytest.ini
77
include pyproject.toml
88
include .pre-commit-config.yaml
99
include nbclient/py.typed

nbclient/tests/test_client.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import warnings
1010
from base64 import b64decode, b64encode
1111
from queue import Empty
12+
from typing import Any
1213
from unittest.mock import MagicMock, Mock
1314

1415
import nbformat
@@ -78,11 +79,15 @@ class AsyncMock(Mock):
7879
pass
7980

8081

81-
def make_async(mock_value):
82-
async def _():
83-
return mock_value
84-
85-
return _()
82+
def make_future(obj: Any) -> asyncio.Future:
83+
try:
84+
loop = asyncio.get_running_loop()
85+
except RuntimeError:
86+
loop = asyncio.new_event_loop()
87+
asyncio.set_event_loop(loop)
88+
future: asyncio.Future = asyncio.Future(loop=loop)
89+
future.set_result(obj)
90+
return future
8691

8792

8893
def normalize_base64(b64_text):
@@ -169,7 +174,7 @@ def shell_channel_message_mock():
169174
# Return the message generator for
170175
# self.kc.shell_channel.get_msg => {'parent_header': {'msg_id': parent_id}}
171176
return AsyncMock(
172-
return_value=make_async(
177+
return_value=make_future(
173178
NBClientTestsBase.merge_dicts(
174179
{
175180
'parent_header': {'msg_id': parent_id},
@@ -186,7 +191,7 @@ def iopub_messages_mock():
186191
return AsyncMock(
187192
side_effect=[
188193
# Default the parent_header so mocks don't need to include this
189-
make_async(
194+
make_future(
190195
NBClientTestsBase.merge_dicts({'parent_header': {'msg_id': parent_id}}, msg)
191196
)
192197
for msg in messages
@@ -215,7 +220,7 @@ def test_mock_wrapper(self):
215220
iopub_channel=MagicMock(get_msg=message_mock),
216221
shell_channel=MagicMock(get_msg=shell_channel_message_mock()),
217222
execute=MagicMock(return_value=parent_id),
218-
is_alive=MagicMock(return_value=make_async(True)),
223+
is_alive=MagicMock(return_value=make_future(True)),
219224
)
220225
executor.parent_id = parent_id
221226
return func(self, executor, cell_mock, message_mock)
@@ -387,11 +392,15 @@ def test_async_parallel_notebooks(capfd, tmpdir):
387392
res = notebook_resources()
388393

389394
with modified_env({"NBEXECUTE_TEST_PARALLEL_TMPDIR": str(tmpdir)}):
390-
tasks = [
391-
async_run_notebook(input_file.format(label=label), opts, res) for label in ("A", "B")
392-
]
393-
loop = asyncio.get_event_loop()
394-
loop.run_until_complete(asyncio.gather(*tasks))
395+
396+
async def run_tasks():
397+
tasks = [
398+
async_run_notebook(input_file.format(label=label), opts, res)
399+
for label in ("A", "B")
400+
]
401+
await asyncio.gather(*tasks)
402+
403+
asyncio.run(run_tasks())
395404

396405
captured = capfd.readouterr()
397406
assert filter_messages_on_error_output(captured.err) == ""
@@ -412,9 +421,11 @@ def test_many_async_parallel_notebooks(capfd):
412421
# run once, to trigger creating the original context
413422
run_notebook(input_file, opts, res)
414423

415-
tasks = [async_run_notebook(input_file, opts, res) for i in range(4)]
416-
loop = asyncio.get_event_loop()
417-
loop.run_until_complete(asyncio.gather(*tasks))
424+
async def run_tasks():
425+
tasks = [async_run_notebook(input_file, opts, res) for i in range(4)]
426+
await asyncio.gather(*tasks)
427+
428+
asyncio.run(run_tasks())
418429

419430
captured = capfd.readouterr()
420431
assert filter_messages_on_error_output(captured.err) == ""
@@ -966,7 +977,7 @@ def message_seq(messages):
966977

967978
message_mock.side_effect = message_seq(list(message_mock.side_effect)[:-1])
968979
executor.kc.shell_channel.get_msg = Mock(
969-
return_value=make_async({'parent_header': {'msg_id': executor.parent_id}})
980+
return_value=make_future({'parent_header': {'msg_id': executor.parent_id}})
970981
)
971982
executor.raise_on_iopub_timeout = True
972983

nbclient/tests/test_util.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ async def some_async_function():
1414

1515

1616
def test_nested_asyncio_with_existing_ioloop():
17-
ioloop = asyncio.new_event_loop()
18-
try:
19-
asyncio.set_event_loop(ioloop)
17+
async def _test():
2018
assert some_async_function() == 42
21-
assert asyncio.get_event_loop() is ioloop
19+
return asyncio.get_running_loop()
20+
21+
loop = asyncio.new_event_loop()
22+
asyncio.set_event_loop(loop)
23+
try:
24+
event_loop = loop.run_until_complete(_test())
25+
assert event_loop is loop
2226
finally:
2327
asyncio._set_running_loop(None) # it seems nest_asyncio doesn't reset this
2428

nbclient/util.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@ def check_patch_tornado() -> None:
3939

4040
def just_run(coro: Awaitable) -> Any:
4141
"""Make the coroutine run, even if there is an event loop running (using nest_asyncio)"""
42-
# original from vaex/asyncio.py
43-
loop = asyncio._get_running_loop()
42+
try:
43+
loop = asyncio.get_running_loop()
44+
except RuntimeError:
45+
loop = None
4446
if loop is None:
4547
had_running_loop = False
46-
try:
47-
loop = asyncio.get_event_loop()
48-
except RuntimeError:
49-
# we can still get 'There is no current event loop in ...'
50-
loop = asyncio.new_event_loop()
51-
asyncio.set_event_loop(loop)
48+
loop = asyncio.new_event_loop()
49+
asyncio.set_event_loop(loop)
5250
else:
5351
had_running_loop = True
5452
if had_running_loop:

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
asyncio_mode = auto

0 commit comments

Comments
 (0)