Skip to content

Commit a4e3750

Browse files
committed
addresses #20
1 parent 93ed04d commit a4e3750

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

pylsp_mypy/plugin.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import shutil
1717
import subprocess
1818
import tempfile
19+
from configparser import ConfigParser
1920
from pathlib import Path
2021
from typing import IO, Any, Dict, List, Optional
2122

@@ -357,7 +358,7 @@ def init(workspace: str) -> Dict[str, str]:
357358

358359
configuration = {}
359360
path = findConfigFile(
360-
workspace, ["pylsp-mypy.cfg", "mypy-ls.cfg", "mypy_ls.cfg", "pyproject.toml"]
361+
workspace, ["pylsp-mypy.cfg", "mypy-ls.cfg", "mypy_ls.cfg", "pyproject.toml"], False
361362
)
362363
if path:
363364
if "pyproject.toml" in path:
@@ -366,14 +367,16 @@ def init(workspace: str) -> Dict[str, str]:
366367
with open(path) as file:
367368
configuration = ast.literal_eval(file.read())
368369

369-
mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini", "pyproject.toml"])
370+
mypyConfigFile = findConfigFile(
371+
workspace, ["mypy.ini", ".mypy.ini", "pyproject.toml", "setup.cfg"], True
372+
)
370373
mypyConfigFileMap[workspace] = mypyConfigFile
371374

372375
log.info("mypyConfigFile = %s configuration = %s", mypyConfigFile, configuration)
373376
return configuration
374377

375378

376-
def findConfigFile(path: str, names: List[str]) -> Optional[str]:
379+
def findConfigFile(path: str, names: List[str], mypy: bool) -> Optional[str]:
377380
"""
378381
Search for a config file.
379382
@@ -386,6 +389,8 @@ def findConfigFile(path: str, names: List[str]) -> Optional[str]:
386389
The path where the search starts.
387390
names : List[str]
388391
The file to be found (or alternative names).
392+
mypy : bool
393+
whether the config file searched is for mypy (plugin otherwise)
389394
390395
Returns
391396
-------
@@ -404,17 +409,28 @@ def findConfigFile(path: str, names: List[str]) -> Optional[str]:
404409
"config file to pylsp-mypy.cfg or preferably use a pyproject.toml instead."
405410
)
406411
if file.name == "pyproject.toml":
407-
isPluginConfig = "pylsp-mypy.cfg" in names
408412
configPresent = (
409-
toml.load(file)
410-
.get("tool", {})
411-
.get("pylsp-mypy" if isPluginConfig else "mypy")
413+
toml.load(file).get("tool", {}).get("mypy" if mypy else "pylsp-mypy")
412414
is not None
413415
)
414416
if not configPresent:
415417
continue
418+
if file.name == "setup.cfg":
419+
config = ConfigParser()
420+
config.read(str(file))
421+
if "mypy" not in config:
422+
continue
416423
return str(file)
417-
424+
# No config file found in the whole directory tree
425+
# -> check mypy default locations for mypy config
426+
if mypy:
427+
defaultPaths = ["~/.config/mypy/config", "~/.mypy.ini"]
428+
XDG_CONFIG_HOME = os.environ.get("XDG_CONFIG_HOME")
429+
if XDG_CONFIG_HOME:
430+
defaultPaths.insert(0, f"{XDG_CONFIG_HOME}/mypy/config")
431+
for path in defaultPaths:
432+
if Path(path).expanduser().exists():
433+
return str(Path(path).expanduser())
418434
return None
419435

420436

0 commit comments

Comments
 (0)