16
16
import shutil
17
17
import subprocess
18
18
import tempfile
19
+ from configparser import ConfigParser
19
20
from pathlib import Path
20
21
from typing import IO , Any , Dict , List , Optional
21
22
@@ -357,7 +358,7 @@ def init(workspace: str) -> Dict[str, str]:
357
358
358
359
configuration = {}
359
360
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
361
362
)
362
363
if path :
363
364
if "pyproject.toml" in path :
@@ -366,14 +367,16 @@ def init(workspace: str) -> Dict[str, str]:
366
367
with open (path ) as file :
367
368
configuration = ast .literal_eval (file .read ())
368
369
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
+ )
370
373
mypyConfigFileMap [workspace ] = mypyConfigFile
371
374
372
375
log .info ("mypyConfigFile = %s configuration = %s" , mypyConfigFile , configuration )
373
376
return configuration
374
377
375
378
376
- def findConfigFile (path : str , names : List [str ]) -> Optional [str ]:
379
+ def findConfigFile (path : str , names : List [str ], mypy : bool ) -> Optional [str ]:
377
380
"""
378
381
Search for a config file.
379
382
@@ -386,6 +389,8 @@ def findConfigFile(path: str, names: List[str]) -> Optional[str]:
386
389
The path where the search starts.
387
390
names : List[str]
388
391
The file to be found (or alternative names).
392
+ mypy : bool
393
+ whether the config file searched is for mypy (plugin otherwise)
389
394
390
395
Returns
391
396
-------
@@ -404,17 +409,28 @@ def findConfigFile(path: str, names: List[str]) -> Optional[str]:
404
409
"config file to pylsp-mypy.cfg or preferably use a pyproject.toml instead."
405
410
)
406
411
if file .name == "pyproject.toml" :
407
- isPluginConfig = "pylsp-mypy.cfg" in names
408
412
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" )
412
414
is not None
413
415
)
414
416
if not configPresent :
415
417
continue
418
+ if file .name == "setup.cfg" :
419
+ config = ConfigParser ()
420
+ config .read (str (file ))
421
+ if "mypy" not in config :
422
+ continue
416
423
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 ())
418
434
return None
419
435
420
436
0 commit comments