Skip to content

Commit a62fdac

Browse files
committed
add default mypy config file feature
1 parent 79057cc commit a62fdac

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

pylsp_mypy/plugin.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,10 @@ def init(workspace: str) -> Dict[str, str]:
278278

279279
mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini"])
280280
mypyConfigFileMap[workspace] = mypyConfigFile
281+
if mypyConfigFileMap[workspace] is None:
282+
mypyConfigFileMap[workspace] = defaultMypyConfigFile()
281283

282-
log.info("mypyConfigFile = %s configuration = %s", mypyConfigFile, configuration)
284+
log.info("mypyConfigFile = %s configuration = %s", mypyConfigFileMap[workspace], configuration)
283285
return configuration
284286

285287

@@ -320,6 +322,30 @@ def findConfigFile(path: str, names: List[str]) -> Optional[str]:
320322
return None
321323

322324

325+
def defaultMypyConfigFile() -> Optional[str]:
326+
"""
327+
Find the mypy default config file path defined in the mypy document below.
328+
329+
Reference: https://mypy.readthedocs.io/en/stable/config_file.html
330+
331+
Returns
332+
-------
333+
Optional[str]
334+
The path where the default config file has been found or None if no matching file has been found.
335+
336+
"""
337+
# A list of user default config file path
338+
user_default_config = ["~/.config/mypy/config", "~/.mypy.ini"]
339+
if os.environ.get("XDG_CONFIG_HOME"):
340+
user_default_config.insert(0, os.path.join(os.environ["XDG_CONFIG_HOME"], "mypy/config"))
341+
342+
for config_file in user_default_config:
343+
if Path(config_file).expanduser().exists():
344+
return config_file
345+
346+
return None
347+
348+
323349
@atexit.register
324350
def close() -> None:
325351
"""

test/test_plugin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pylsp.workspace import Workspace, Document
44
from pylsp.config.config import Config
55
from pylsp import uris
6-
from unittest.mock import Mock
6+
from unittest.mock import Mock, patch
77
from pylsp_mypy import plugin
88

99
DOC_URI = __file__
@@ -20,6 +20,7 @@
2020
def workspace(tmpdir):
2121
"""Return a workspace."""
2222
ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
23+
ws._root_path = "C:"
2324
ws._config = Config(ws.root_uri, {}, 0, {})
2425
return ws
2526

@@ -120,3 +121,12 @@ def foo():
120121
doc2 = Document(DOC_URI, ws2, DOC_SOURCE)
121122
diags = plugin.pylsp_lint(ws2._config, ws2, doc2, is_saved=False)
122123
assert len(diags) == 0
124+
125+
126+
def test_default_config_file(tmpdir):
127+
config = FakeConfig()
128+
with patch("pylsp_mypy.plugin.findConfigFile", return_value=None), patch(
129+
"pathlib.Path.exists", return_value=True
130+
):
131+
plugin.pylsp_settings(config)
132+
assert plugin.mypyConfigFileMap.get(config._root_path) == "~/.config/mypy/config"

0 commit comments

Comments
 (0)