Skip to content

Commit 0a1a3bf

Browse files
committed
Fix PythonSpec raising error when module is not installed
1 parent 2508faa commit 0a1a3bf

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

python_packages/jupyter_lsp/jupyter_lsp/specs/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class PythonModuleSpec(SpecBase):
9393
def __call__(self, mgr: LanguageServerManagerAPI) -> KeyedLanguageServerSpecs:
9494
spec = __import__("importlib").util.find_spec(self.python_module)
9595

96+
if not spec:
97+
return {}
98+
9699
if not spec.origin: # pragma: no cover
97100
return {}
98101

python_packages/jupyter_lsp/jupyter_lsp/tests/test_detect.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import shutil
22

33
from jupyter_lsp.specs.r_languageserver import RLanguageServer
4+
from jupyter_lsp.specs.utils import PythonModuleSpec
45

56

67
def test_no_detect(manager):
@@ -28,3 +29,23 @@ class NonInstalledRServer(RLanguageServer):
2829

2930
non_installed_server = NonInstalledRServer()
3031
assert non_installed_server.is_installed(cmd=existing_runner) is False
32+
33+
34+
def test_missing_python_module_spec():
35+
"""Prevent failure in module detection raising error:
36+
37+
Failed to fetch commands from language server spec finder`python-language-server`:
38+
'NoneType' object has no attribute 'origin'
39+
Traceback (most recent call last):
40+
File "../lib/python3.8/site-packages/jupyter_lsp/manager.py", line 209, in _autodetect_language_servers
41+
specs = spec_finder(self)
42+
File "../lib/python3.8/site-packages/jupyter_lsp/specs/utils.py", line 96, in __call__
43+
if not spec.origin: # pragma: no cover
44+
AttributeError: 'NoneType' object has no attribute 'origin'
45+
"""
46+
47+
class NonInstalledPythonServer(PythonModuleSpec):
48+
python_module = "not_installed_python_module"
49+
50+
not_installed_server = NonInstalledPythonServer()
51+
assert not_installed_server(mgr=None) == {}

0 commit comments

Comments
 (0)