Skip to content

Commit 3f552f0

Browse files
committed
Create new config option 'config_names'
This configuration option allows us to specify additional configuration file names under which the mypy config could be found.
1 parent 81cf8f4 commit 3f552f0

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Configuration
3636
``overrides`` (default is ``[True]``) specifies a list of alternate or supplemental command-line options.
3737
This modifies the options passed to ``mypy`` or the mypy-specific ones passed to ``dmypy run``. When present, the special boolean member ``True`` is replaced with the command-line options that would've been passed had ``overrides`` not been specified. Later options take precedence, which allows for replacing or negating individual default options (see ``mypy.main:process_options`` and ``mypy --help | grep inverse``).
3838

39+
``config_names`` (default is ``[]``) specifies alternate file names under which the mypy configuration may be found.
40+
3941
This project supports the use of ``pyproject.toml`` for configuration. It is in fact the preferred way. Using that your configuration could look like this:
4042

4143
::
@@ -76,6 +78,15 @@ With ``overrides`` specified (for example to tell mypy to use a different python
7678
"overrides": ["--python-executable", "/home/me/bin/python", True]
7779
}
7880

81+
With ``config_names`` your config could look like this:
82+
83+
::
84+
85+
{
86+
"enabled": True,
87+
"config_files": [".config/mypy.ini"]
88+
}
89+
7990

8091
Developing
8192
-------------

pylsp_mypy/plugin.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ def apply_overrides(args: List[str], overrides: List[Any]) -> List[str]:
119119
return overrides[: -(len(rest) + 1)] + args + rest
120120

121121

122+
_mypy_config_names_cache: Dict[str, List[str]] = {}
123+
124+
125+
def updateMypyConfigFileMap(config: Config, settings: Any) -> None:
126+
"""
127+
Updates the mypyConfigFileMap for new work spaces or when the
128+
'config_names' setting for an existing workspace changes.
129+
"""
130+
workspace = config._root_path
131+
config_names = settings.get("config_names", [])
132+
if (
133+
workspace not in mypyConfigFileMap
134+
or _mypy_config_names_cache.get(workspace, []) != config_names
135+
):
136+
mypyConfigFile = findConfigFile(
137+
workspace, config_names + ["mypy.ini", ".mypy.ini", "pyproject.toml"]
138+
)
139+
mypyConfigFileMap[workspace] = mypyConfigFile
140+
141+
log.info("mypyConfigFile = %s", mypyConfigFile)
142+
143+
122144
@hookimpl
123145
def pylsp_lint(
124146
config: Config, workspace: Workspace, document: Document, is_saved: bool
@@ -166,6 +188,8 @@ def pylsp_lint(
166188
is_saved,
167189
)
168190

191+
updateMypyConfigFileMap(config, settings)
192+
169193
live_mode = settings.get("live_mode", True)
170194
dmypy = settings.get("dmypy", False)
171195

@@ -361,10 +385,7 @@ def init(workspace: str) -> Dict[str, str]:
361385
with open(path) as file:
362386
configuration = ast.literal_eval(file.read())
363387

364-
mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini", "pyproject.toml"])
365-
mypyConfigFileMap[workspace] = mypyConfigFile
366-
367-
log.info("mypyConfigFile = %s configuration = %s", mypyConfigFile, configuration)
388+
log.info("configuration = %s", configuration)
368389
return configuration
369390

370391

test/test_plugin.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,34 @@ def test_option_overrides_dmypy(last_diagnostics_monkeypatch, workspace):
222222
document.path,
223223
]
224224
m.assert_called_with(expected, stderr=-1, stdout=-1, **windows_flag)
225+
226+
227+
def test_config_names(tmpdir, last_diagnostics_monkeypatch):
228+
DOC_SOURCE = """
229+
def foo():
230+
return
231+
unreachable = 1
232+
"""
233+
DOC_ERR_MSG = "Statement is unreachable"
234+
235+
config_names = [".config/mypy.ini"]
236+
237+
# Initialize workspace.
238+
ws = Workspace(uris.from_fs_path(str(tmpdir)), Mock())
239+
ws._config = Config(ws.root_uri, {}, 0, {})
240+
241+
# Create configuration file for workspace.
242+
config_dir = tmpdir.mkdir(".config")
243+
mypy_config = config_dir.join("mypy.ini")
244+
mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")
245+
246+
# Update settings for workspace.
247+
plugin.pylsp_settings(ws._config)
248+
ws.update_config({"pylsp": {"plugins": {"pylsp_mypy": {"config_names": config_names}}}})
249+
250+
# Test document to make sure it uses .config/mypy.ini configuration.
251+
doc = Document(DOC_URI, ws, DOC_SOURCE)
252+
diags = plugin.pylsp_lint(ws._config, ws, doc, is_saved=False)
253+
assert len(diags) == 1
254+
diag = diags[0]
255+
assert diag["message"] == DOC_ERR_MSG

0 commit comments

Comments
 (0)