Skip to content

Commit feb651e

Browse files
tkrabel-dbstaticf0x
authored andcommitted
Make workspace/didChangeConfig work with notebook documents (python-lsp#462)
1 parent 60ba623 commit feb651e

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

Diff for: pylsp/workspace.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ def update_document(self, doc_uri, change, version=None):
167167
def update_config(self, settings):
168168
self._config.update((settings or {}).get("pylsp", {}))
169169
for doc_uri in self.documents:
170-
self.get_document(doc_uri).update_config(settings)
170+
if isinstance(document := self.get_document(doc_uri), Notebook):
171+
# Notebook documents don't have a config. The config is
172+
# handled at the cell level.
173+
return
174+
document.update_config(settings)
171175

172176
def apply_edit(self, edit):
173177
return self._endpoint.request(self.M_APPLY_EDIT, {"edit": edit})

Diff for: test/test_notebook_document.py

+55-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import os
44
import time
5-
from unittest.mock import patch, call
65

6+
from unittest.mock import patch, call
77
from test.fixtures import CALL_TIMEOUT_IN_SECONDS
8-
98
import pytest
9+
from pylsp.workspace import Notebook
1010

1111
from pylsp import IS_WIN
1212
from pylsp.lsp import NotebookCellKind
@@ -37,6 +37,59 @@ def test_initialize(client_server_pair):
3737
assert isinstance(selector, list)
3838

3939

40+
@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows")
41+
def test_workspace_did_change_configuration(client_server_pair):
42+
"""Test that we can update a workspace config w/o error when a notebook is open."""
43+
client, server = client_server_pair
44+
client._endpoint.request(
45+
"initialize",
46+
{
47+
"processId": 1234,
48+
"rootPath": os.path.dirname(__file__),
49+
},
50+
).result(timeout=CALL_TIMEOUT_IN_SECONDS)
51+
assert server.workspace is not None
52+
53+
with patch.object(server._endpoint, "notify") as mock_notify:
54+
client._endpoint.notify(
55+
"notebookDocument/didOpen",
56+
{
57+
"notebookDocument": {
58+
"uri": "notebook_uri",
59+
"notebookType": "jupyter-notebook",
60+
"cells": [
61+
{
62+
"kind": NotebookCellKind.Code,
63+
"document": "cell_1_uri",
64+
},
65+
],
66+
},
67+
"cellTextDocuments": [
68+
{
69+
"uri": "cell_1_uri",
70+
"languageId": "python",
71+
"text": "",
72+
},
73+
],
74+
},
75+
)
76+
wait_for_condition(lambda: mock_notify.call_count >= 1)
77+
assert isinstance(server.workspace.get_document("notebook_uri"), Notebook)
78+
assert len(server.workspace.documents) == 2
79+
80+
server.workspace.update_config(
81+
{"pylsp": {"plugins": {"flake8": {"enabled": True}}}}
82+
)
83+
84+
assert server.config.plugin_settings("flake8").get("enabled") is True
85+
assert (
86+
server.workspace.get_document("cell_1_uri")
87+
._config.plugin_settings("flake8")
88+
.get("enabled")
89+
is True
90+
)
91+
92+
4093
@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows")
4194
def test_notebook_document__did_open(
4295
client_server_pair,

0 commit comments

Comments
 (0)