Skip to content

Commit 7382916

Browse files
committed
toml support
1 parent 95009dd commit 7382916

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

pylsp_mypy/plugin.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from pathlib import Path
2121
from typing import IO, Any, Dict, List, Optional
2222

23+
import toml
2324
from mypy import api as mypy_api
2425
from pylsp import hookimpl
2526
from pylsp.config.config import Config
@@ -324,12 +325,17 @@ def init(workspace: str) -> Dict[str, str]:
324325
log.info("init workspace = %s", workspace)
325326

326327
configuration = {}
327-
path = findConfigFile(workspace, ["pylsp-mypy.cfg", "mypy-ls.cfg", "mypy_ls.cfg"])
328+
path = findConfigFile(
329+
workspace, ["pylsp-mypy.cfg", "mypy-ls.cfg", "mypy_ls.cfg", "pyproject.toml"]
330+
)
328331
if path:
329-
with open(path) as file:
330-
configuration = ast.literal_eval(file.read())
332+
if "pyproject.toml" in path:
333+
configuration = toml.load(path).get("tool").get("pylsp-mypy")
334+
else:
335+
with open(path) as file:
336+
configuration = ast.literal_eval(file.read())
331337

332-
mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini"])
338+
mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini", "pyproject.toml"])
333339
mypyConfigFileMap[workspace] = mypyConfigFile
334340

335341
log.info("mypyConfigFile = %s configuration = %s", mypyConfigFile, configuration)
@@ -368,6 +374,16 @@ def findConfigFile(path: str, names: List[str]) -> Optional[str]:
368374
"config file to pylsp-mypy.cfg"
369375
)
370376
)
377+
if file.name == "pyproject.toml":
378+
isPluginConfig = "pylsp-mypy.cfg" in names
379+
configPresent = (
380+
toml.load(file)
381+
.get("tool", {})
382+
.get("pylsp-mypy" if isPluginConfig else "mypy")
383+
is None
384+
)
385+
if not configPresent:
386+
continue
371387
return str(file)
372388

373389
return None

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ exclude = '''
1616
[tool.isort]
1717
profile = "black"
1818
line_length = 100
19+
20+
[tool.pylsp-mypy]
21+
enabled = true
22+
live_mode = true
23+
strict = true

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
python-lsp-server
22
mypy
3+
toml
34
black
45
pre-commit
56
rstcheck

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ packages = find:
2222
install_requires =
2323
python-lsp-server
2424
mypy
25+
toml
2526

2627

2728
[options.entry_points]

0 commit comments

Comments
 (0)