|
20 | 20 | from pathlib import Path |
21 | 21 | from typing import IO, Any, Dict, List, Optional |
22 | 22 |
|
| 23 | +import toml |
23 | 24 | from mypy import api as mypy_api |
24 | 25 | from pylsp import hookimpl |
25 | 26 | from pylsp.config.config import Config |
@@ -324,12 +325,17 @@ def init(workspace: str) -> Dict[str, str]: |
324 | 325 | log.info("init workspace = %s", workspace) |
325 | 326 |
|
326 | 327 | 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 | + ) |
328 | 331 | 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()) |
331 | 337 |
|
332 | | - mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini"]) |
| 338 | + mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini", "pyproject.toml"]) |
333 | 339 | mypyConfigFileMap[workspace] = mypyConfigFile |
334 | 340 |
|
335 | 341 | log.info("mypyConfigFile = %s configuration = %s", mypyConfigFile, configuration) |
@@ -368,6 +374,16 @@ def findConfigFile(path: str, names: List[str]) -> Optional[str]: |
368 | 374 | "config file to pylsp-mypy.cfg" |
369 | 375 | ) |
370 | 376 | ) |
| 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 |
371 | 387 | return str(file) |
372 | 388 |
|
373 | 389 | return None |
|
0 commit comments