Skip to content

Commit 0003369

Browse files
committed
add config cache
1 parent 77070a0 commit 0003369

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

pylsp_mypy/plugin.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
# A mapping from workspace path to config file path
3838
mypyConfigFileMap: Dict[str, Optional[str]] = {}
3939

40+
settingsCache: Dict[str, Dict[str, Any]] = {}
41+
4042
tmpFile: Optional[IO[str]] = None
4143

4244
# In non-live-mode the file contents aren't updated.
@@ -124,6 +126,20 @@ def apply_overrides(args: List[str], overrides: List[Any]) -> List[str]:
124126
return overrides[: -(len(rest) + 1)] + args + rest
125127

126128

129+
def didSettingsChange(workspace: str, settings: Dict[str, Any]) -> None:
130+
"""Handle relevant changes to the settings between runs."""
131+
configSubPaths = settings.get("config_sub_paths", [])
132+
if settingsCache[workspace].get("config_sub_paths", []) != configSubPaths:
133+
mypyConfigFile = findConfigFile(
134+
workspace,
135+
configSubPaths,
136+
["mypy.ini", ".mypy.ini", "pyproject.toml", "setup.cfg"],
137+
True,
138+
)
139+
mypyConfigFileMap[workspace] = mypyConfigFile
140+
settingsCache[workspace] = settings.copy()
141+
142+
127143
@hookimpl
128144
def pylsp_lint(
129145
config: Config, workspace: Workspace, document: Document, is_saved: bool
@@ -161,15 +177,16 @@ def pylsp_lint(
161177
if settings == {}:
162178
settings = oldSettings2
163179

180+
didSettingsChange(workspace.root_path, settings)
181+
164182
if settings.get("report_progress", False):
165183
with workspace.report_progress("lint: mypy"):
166-
return get_diagnostics(config, workspace, document, settings, is_saved)
184+
return get_diagnostics(workspace, document, settings, is_saved)
167185
else:
168-
return get_diagnostics(config, workspace, document, settings, is_saved)
186+
return get_diagnostics(workspace, document, settings, is_saved)
169187

170188

171189
def get_diagnostics(
172-
config: Config,
173190
workspace: Workspace,
174191
document: Document,
175192
settings: Dict[str, Any],
@@ -180,8 +197,6 @@ def get_diagnostics(
180197
181198
Parameters
182199
----------
183-
config : Config
184-
The pylsp config.
185200
workspace : Workspace
186201
The pylsp workspace.
187202
document : Document
@@ -415,6 +430,7 @@ def init(workspace: str) -> Dict[str, str]:
415430
workspace, configSubPaths, ["mypy.ini", ".mypy.ini", "pyproject.toml", "setup.cfg"], True
416431
)
417432
mypyConfigFileMap[workspace] = mypyConfigFile
433+
settingsCache[workspace] = configuration.copy()
418434

419435
log.info("mypyConfigFile = %s configuration = %s", mypyConfigFile, configuration)
420436
return configuration

test/test_plugin.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,17 @@ def foo():
112112

113113
# Initialize two workspace folders.
114114
folder1 = tmpdir.mkdir("folder1")
115-
ws1 = Workspace(uris.from_fs_path(str(folder1)), Mock())
116-
ws1._config = Config(ws1.root_uri, {}, 0, {})
117115
folder2 = tmpdir.mkdir("folder2")
118-
ws2 = Workspace(uris.from_fs_path(str(folder2)), Mock())
119-
ws2._config = Config(ws2.root_uri, {}, 0, {})
120116

121117
# Create configuration file for workspace folder 1.
122118
mypy_config = folder1.join("mypy.ini")
123119
mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")
124120

121+
ws1 = Workspace(uris.from_fs_path(str(folder1)), Mock())
122+
ws1._config = Config(ws1.root_uri, {}, 0, {})
123+
ws2 = Workspace(uris.from_fs_path(str(folder2)), Mock())
124+
ws2._config = Config(ws2.root_uri, {}, 0, {})
125+
125126
# Initialize settings for both folders.
126127
plugin.pylsp_settings(ws1._config)
127128
plugin.pylsp_settings(ws2._config)
@@ -265,19 +266,51 @@ def foo():
265266

266267
config_sub_paths = [".config"]
267268

269+
# Create configuration file for workspace.
270+
plugin_config = tmpdir.join("pyproject.toml")
271+
plugin_config.write(f"[tool.pylsp-mypy]\nenabled = true\nconfig_sub_paths = {config_sub_paths}")
272+
config_dir = tmpdir.mkdir(".config")
273+
mypy_config = config_dir.join("mypy.ini")
274+
mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")
275+
268276
# Initialize workspace.
277+
269278
ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
270279
ws._config = Config(ws.root_uri, {}, 0, {})
271280

281+
# Update settings for workspace.
282+
plugin.pylsp_settings(ws._config)
283+
284+
# Test document to make sure it uses .config/mypy.ini configuration.
285+
doc = Document(DOC_URI, ws, DOC_SOURCE)
286+
diags = plugin.pylsp_lint(ws._config, ws, doc, is_saved=False)
287+
assert len(diags) == 1
288+
diag = diags[0]
289+
assert diag["message"] == DOC_ERR_MSG
290+
291+
292+
def test_config_sub_paths_config_changed(tmpdir, last_diagnostics_monkeypatch):
293+
DOC_SOURCE = """
294+
def foo():
295+
return
296+
unreachable = 1
297+
"""
298+
DOC_ERR_MSG = "Statement is unreachable [unreachable]"
299+
272300
# Create configuration file for workspace.
273-
plugin_config = tmpdir.join("pyproject.toml")
274-
plugin_config.write(f"[tool.pylsp-mypy]\nenabled = true\nconfig_sub_paths = {config_sub_paths}")
275301
config_dir = tmpdir.mkdir(".config")
276302
mypy_config = config_dir.join("mypy.ini")
277303
mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")
278304

305+
config_sub_paths = [".config"]
306+
307+
# Initialize workspace.
308+
ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
309+
ws._config = Config(ws.root_uri, {}, 0, {})
310+
279311
# Update settings for workspace.
280312
plugin.pylsp_settings(ws._config)
313+
ws.update_config({"pylsp": {"plugins": {"pylsp_mypy": {"config_sub_paths": config_sub_paths}}}})
281314

282315
# Test document to make sure it uses .config/mypy.ini configuration.
283316
doc = Document(DOC_URI, ws, DOC_SOURCE)

0 commit comments

Comments
 (0)