Skip to content

Make workspace/didChangeConfig work with notebook documents #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pylsp/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ def update_document(self, doc_uri, change, version=None):
def update_config(self, settings):
self._config.update((settings or {}).get("pylsp", {}))
for doc_uri in self.documents:
self.get_document(doc_uri).update_config(settings)
if isinstance(document := self.get_document(doc_uri), Notebook):
# Notebook documents don't have a config. The config is
# handled at the cell level.
return
document.update_config(settings)

def apply_edit(self, edit):
return self._endpoint.request(self.M_APPLY_EDIT, {"edit": edit})
Expand Down
57 changes: 55 additions & 2 deletions test/test_notebook_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import os
import time
from unittest.mock import patch, call

from unittest.mock import patch, call
from test.fixtures import CALL_TIMEOUT_IN_SECONDS

import pytest
from pylsp.workspace import Notebook

from pylsp import IS_WIN
from pylsp.lsp import NotebookCellKind
Expand Down Expand Up @@ -37,6 +37,59 @@ def test_initialize(client_server_pair):
assert isinstance(selector, list)


@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows")
def test_workspace_did_change_configuration(client_server_pair):
"""Test that we can update a workspace config w/o error when a notebook is open."""
client, server = client_server_pair
client._endpoint.request(
"initialize",
{
"processId": 1234,
"rootPath": os.path.dirname(__file__),
},
).result(timeout=CALL_TIMEOUT_IN_SECONDS)
assert server.workspace is not None

with patch.object(server._endpoint, "notify") as mock_notify:
client._endpoint.notify(
"notebookDocument/didOpen",
{
"notebookDocument": {
"uri": "notebook_uri",
"notebookType": "jupyter-notebook",
"cells": [
{
"kind": NotebookCellKind.Code,
"document": "cell_1_uri",
},
],
},
"cellTextDocuments": [
{
"uri": "cell_1_uri",
"languageId": "python",
"text": "",
},
],
},
)
wait_for_condition(lambda: mock_notify.call_count >= 1)
assert isinstance(server.workspace.get_document("notebook_uri"), Notebook)
assert len(server.workspace.documents) == 2

server.workspace.update_config(
{"pylsp": {"plugins": {"flake8": {"enabled": True}}}}
)

assert server.config.plugin_settings("flake8").get("enabled") is True
assert (
server.workspace.get_document("cell_1_uri")
._config.plugin_settings("flake8")
.get("enabled")
is True
)


@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows")
def test_notebook_document__did_open(
client_server_pair,
Expand Down