Skip to content

Add default mypy config file reference #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion pylsp_mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,10 @@ def init(workspace: str) -> Dict[str, str]:

mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini"])
mypyConfigFileMap[workspace] = mypyConfigFile
if mypyConfigFileMap[workspace] is None:
mypyConfigFileMap[workspace] = defaultMypyConfigFile()

log.info("mypyConfigFile = %s configuration = %s", mypyConfigFile, configuration)
log.info("mypyConfigFile = %s configuration = %s", mypyConfigFileMap[workspace], configuration)
return configuration


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


def defaultMypyConfigFile() -> Optional[str]:
"""
Find the mypy default config file path defined in the mypy document below.

Reference: https://mypy.readthedocs.io/en/stable/config_file.html

Returns
-------
Optional[str]
The path where the default config file has been found or None if no matching file has been found.

"""
# A list of user default config file path
user_default_config = ["~/.config/mypy/config", "~/.mypy.ini"]
if os.environ.get("XDG_CONFIG_HOME"):
user_default_config.insert(0, os.path.join(os.environ["XDG_CONFIG_HOME"], "mypy/config"))

for config_file in user_default_config:
if Path(config_file).expanduser().exists():
return config_file

return None


@atexit.register
def close() -> None:
"""
Expand Down
12 changes: 11 additions & 1 deletion test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pylsp.workspace import Workspace, Document
from pylsp.config.config import Config
from pylsp import uris
from unittest.mock import Mock
from unittest.mock import Mock, patch
from pylsp_mypy import plugin

DOC_URI = __file__
Expand All @@ -20,6 +20,7 @@
def workspace(tmpdir):
"""Return a workspace."""
ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
ws._root_path = "C:"
ws._config = Config(ws.root_uri, {}, 0, {})
return ws

Expand Down Expand Up @@ -120,3 +121,12 @@ def foo():
doc2 = Document(DOC_URI, ws2, DOC_SOURCE)
diags = plugin.pylsp_lint(ws2._config, ws2, doc2, is_saved=False)
assert len(diags) == 0


def test_default_config_file(tmpdir):
config = FakeConfig()
with patch("pylsp_mypy.plugin.findConfigFile", return_value=None), patch(
"pathlib.Path.exists", return_value=True
):
plugin.pylsp_settings(config)
assert plugin.mypyConfigFileMap.get(config._root_path) == "~/.config/mypy/config"