Skip to content

Commit

Permalink
add config cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Richardk2n committed Feb 25, 2023
1 parent 77070a0 commit 0003369
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
26 changes: 21 additions & 5 deletions pylsp_mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
# A mapping from workspace path to config file path
mypyConfigFileMap: Dict[str, Optional[str]] = {}

settingsCache: Dict[str, Dict[str, Any]] = {}

tmpFile: Optional[IO[str]] = None

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


def didSettingsChange(workspace: str, settings: Dict[str, Any]) -> None:
"""Handle relevant changes to the settings between runs."""
configSubPaths = settings.get("config_sub_paths", [])
if settingsCache[workspace].get("config_sub_paths", []) != configSubPaths:
mypyConfigFile = findConfigFile(
workspace,
configSubPaths,
["mypy.ini", ".mypy.ini", "pyproject.toml", "setup.cfg"],
True,
)
mypyConfigFileMap[workspace] = mypyConfigFile
settingsCache[workspace] = settings.copy()


@hookimpl
def pylsp_lint(
config: Config, workspace: Workspace, document: Document, is_saved: bool
Expand Down Expand Up @@ -161,15 +177,16 @@ def pylsp_lint(
if settings == {}:
settings = oldSettings2

didSettingsChange(workspace.root_path, settings)

if settings.get("report_progress", False):
with workspace.report_progress("lint: mypy"):
return get_diagnostics(config, workspace, document, settings, is_saved)
return get_diagnostics(workspace, document, settings, is_saved)
else:
return get_diagnostics(config, workspace, document, settings, is_saved)
return get_diagnostics(workspace, document, settings, is_saved)


def get_diagnostics(
config: Config,
workspace: Workspace,
document: Document,
settings: Dict[str, Any],
Expand All @@ -180,8 +197,6 @@ def get_diagnostics(
Parameters
----------
config : Config
The pylsp config.
workspace : Workspace
The pylsp workspace.
document : Document
Expand Down Expand Up @@ -415,6 +430,7 @@ def init(workspace: str) -> Dict[str, str]:
workspace, configSubPaths, ["mypy.ini", ".mypy.ini", "pyproject.toml", "setup.cfg"], True
)
mypyConfigFileMap[workspace] = mypyConfigFile
settingsCache[workspace] = configuration.copy()

log.info("mypyConfigFile = %s configuration = %s", mypyConfigFile, configuration)
return configuration
Expand Down
45 changes: 39 additions & 6 deletions test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,17 @@ def foo():

# Initialize two workspace folders.
folder1 = tmpdir.mkdir("folder1")
ws1 = Workspace(uris.from_fs_path(str(folder1)), Mock())
ws1._config = Config(ws1.root_uri, {}, 0, {})
folder2 = tmpdir.mkdir("folder2")
ws2 = Workspace(uris.from_fs_path(str(folder2)), Mock())
ws2._config = Config(ws2.root_uri, {}, 0, {})

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

ws1 = Workspace(uris.from_fs_path(str(folder1)), Mock())
ws1._config = Config(ws1.root_uri, {}, 0, {})
ws2 = Workspace(uris.from_fs_path(str(folder2)), Mock())
ws2._config = Config(ws2.root_uri, {}, 0, {})

# Initialize settings for both folders.
plugin.pylsp_settings(ws1._config)
plugin.pylsp_settings(ws2._config)
Expand Down Expand Up @@ -265,19 +266,51 @@ def foo():

config_sub_paths = [".config"]

# Create configuration file for workspace.
plugin_config = tmpdir.join("pyproject.toml")
plugin_config.write(f"[tool.pylsp-mypy]\nenabled = true\nconfig_sub_paths = {config_sub_paths}")
config_dir = tmpdir.mkdir(".config")
mypy_config = config_dir.join("mypy.ini")
mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")

# Initialize workspace.

ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
ws._config = Config(ws.root_uri, {}, 0, {})

# Update settings for workspace.
plugin.pylsp_settings(ws._config)

# Test document to make sure it uses .config/mypy.ini configuration.
doc = Document(DOC_URI, ws, DOC_SOURCE)
diags = plugin.pylsp_lint(ws._config, ws, doc, is_saved=False)
assert len(diags) == 1
diag = diags[0]
assert diag["message"] == DOC_ERR_MSG


def test_config_sub_paths_config_changed(tmpdir, last_diagnostics_monkeypatch):
DOC_SOURCE = """
def foo():
return
unreachable = 1
"""
DOC_ERR_MSG = "Statement is unreachable [unreachable]"

# Create configuration file for workspace.
plugin_config = tmpdir.join("pyproject.toml")
plugin_config.write(f"[tool.pylsp-mypy]\nenabled = true\nconfig_sub_paths = {config_sub_paths}")
config_dir = tmpdir.mkdir(".config")
mypy_config = config_dir.join("mypy.ini")
mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")

config_sub_paths = [".config"]

# Initialize workspace.
ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
ws._config = Config(ws.root_uri, {}, 0, {})

# Update settings for workspace.
plugin.pylsp_settings(ws._config)
ws.update_config({"pylsp": {"plugins": {"pylsp_mypy": {"config_sub_paths": config_sub_paths}}}})

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

0 comments on commit 0003369

Please sign in to comment.