Skip to content

Commit 2e19c2c

Browse files
committed
Handle async do_complete.
A number of methods of ipykernel can optinally return `awaitable[T]` instead of just `T`, this is the case for `do_complete`. I think it's a mistake ; see ipython/ipykernel#1295 ; in particular because it's easy to forget / hard to properly type-check, and I'd like to make it mandatory in the long term to have await. Spyder seem to not handle the case where do_completer return an awaitable (or more partiularly is `do_complete` is a coroutine function. This tries to handle it – and as of course `do_completer` _can_ be async, all caller _must_ be async. So I try to do all the required updates. Note: I also add explict imports in some test, to get better error message in case those deps are not installed.
1 parent 2793fd3 commit 2e19c2c

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

spyder_kernels/console/tests/test_console_kernel.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,13 +901,15 @@ def test_matplotlib_inline(kernel):
901901
assert 'inline' in value
902902

903903

904-
def test_do_complete(kernel):
904+
async def test_do_complete(kernel):
905905
"""
906906
Check do complete works in normal and debugging mode.
907907
"""
908908
asyncio.run(kernel.do_execute('abba = 1', True))
909909
assert kernel.get_value('abba') == 1
910910
match = kernel.do_complete('ab', 2)
911+
if isawaitable(match):
912+
match = await match
911913
assert 'abba' in match['matches']
912914

913915
# test pdb
@@ -916,6 +918,8 @@ def test_do_complete(kernel):
916918
pdb_obj.completenames = lambda *ignore: ['baba']
917919
kernel.shell._namespace_stack = [pdb_obj]
918920
match = kernel.do_complete('ba', 2)
921+
if isawaitable(match):
922+
match = await match
919923
assert 'baba' in match['matches']
920924
pdb_obj.curframe = None
921925

@@ -1435,6 +1439,7 @@ def test_django_settings(kernel):
14351439
14361440
This is a regression test for issue spyder-ide/spyder#19516
14371441
"""
1442+
import django
14381443
asyncio.run(kernel.do_execute('from django.conf import settings', True))
14391444
nsview = repr(kernel.get_namespace_view())
14401445
assert "'settings':" in nsview

spyder_kernels/customize/spyderpdb.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,16 @@ def do_where(self, arg):
351351
do_bt = do_where
352352

353353
# --- Method defined by us to respond to ipython complete protocol
354-
def do_complete(self, code, cursor_pos):
354+
async def do_complete(self, code, cursor_pos):
355355
"""
356356
Respond to a complete request.
357357
"""
358358
if self.pdb_use_exclamation_mark:
359-
return self._complete_exclamation(code, cursor_pos)
359+
return await self._complete_exclamation(code, cursor_pos)
360360
else:
361-
return self._complete_default(code, cursor_pos)
361+
return await self._complete_default(code, cursor_pos)
362362

363-
def _complete_default(self, code, cursor_pos):
363+
async def _complete_default(self, code, cursor_pos):
364364
"""
365365
Respond to a complete request if not pdb_use_exclamation_mark.
366366
"""
@@ -422,7 +422,7 @@ def is_name_or_composed(text):
422422
else:
423423
frame = self.curframe
424424
self.shell.set_completer_frame(frame)
425-
result = self.shell.kernel._do_complete(code, cursor_pos)
425+
result = await self.shell.kernel._do_complete(code, cursor_pos)
426426
# Reset frame
427427
self.shell.set_completer_frame()
428428
# If there is no Pdb results to merge, return the result
@@ -450,7 +450,7 @@ def is_name_or_composed(text):
450450
'metadata': {},
451451
'status': 'ok'}
452452

453-
def _complete_exclamation(self, code, cursor_pos):
453+
async def _complete_exclamation(self, code, cursor_pos):
454454
"""
455455
Respond to a complete request if pdb_use_exclamation_mark.
456456
"""
@@ -529,7 +529,7 @@ def is_name_or_composed(text):
529529
else:
530530
frame = self.curframe
531531
self.shell.set_completer_frame(frame)
532-
result = self.shell.kernel._do_complete(code, cursor_pos)
532+
result = await self.shell.kernel._do_complete(code, cursor_pos)
533533
# Reset frame
534534
self.shell.set_completer_frame()
535535
return result

spyder_kernels/utils/tests/test_iofuncs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ def test_spydata_export(input_namespace, expected_namespace,
342342

343343
def test_save_load_hdf5_files(tmp_path):
344344
"""Simple test to check that we can save and load HDF5 files."""
345+
import h5py
345346
h5_file = tmp_path / "test.h5"
346347
data = {'a' : [1, 2, 3, 4], 'b' : 4.5}
347348
iofuncs.save_hdf5(data, h5_file)

0 commit comments

Comments
 (0)