Skip to content

Commit adf1572

Browse files
committed
dmypy status-file closes #47
1 parent b21f5af commit adf1572

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ 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+
``dmypy_status_file`` (Default is ``.dmypy.json``) specifies which status file dmypy should use.
40+
This modifies the ``--status-file`` option passed to ``dmypy`` given ``dmypy`` is active.
41+
3942
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:
4043

4144
::
@@ -76,6 +79,17 @@ With ``overrides`` specified (for example to tell mypy to use a different python
7679
"overrides": ["--python-executable", "/home/me/bin/python", True]
7780
}
7881

82+
With ``dmypy_status_file`` your config could look like this:
83+
84+
::
85+
86+
{
87+
"enabled": True,
88+
"live_mode": False,
89+
"dmypy": True,
90+
"strict": False,
91+
"dmypy_status_file": ".custom_dmypy_status_file.json"
92+
}
7993

8094
Developing
8195
-------------

pylsp_mypy/plugin.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
# Windows started opening opening a cmd-like window for every subprocess call
4545
# This flag prevents that.
4646
# This flag is new in python 3.7
47-
# THis flag only exists on Windows
47+
# This flag only exists on Windows
4848
windows_flag: Dict[str, int] = (
4949
{"creationflags": subprocess.CREATE_NO_WINDOW} if os.name == "nt" else {} # type: ignore
5050
)
@@ -175,6 +175,9 @@ def pylsp_lint(
175175
log.warning("live_mode is not supported with dmypy, disabling")
176176
live_mode = False
177177

178+
if dmypy:
179+
dmypy_status_file = settings.get("dmypy_status_file", ".dmypy.json")
180+
178181
args = ["--show-column-numbers"]
179182

180183
global tmpFile
@@ -237,9 +240,11 @@ def pylsp_lint(
237240

238241
if shutil.which("dmypy"):
239242
# dmypy exists on path
240-
# -> use mypy on path
243+
# -> use dmypy on path
241244
completed_process = subprocess.run(
242-
["dmypy", "status"], stderr=subprocess.PIPE, **windows_flag
245+
["dmypy", "--status-file", dmypy_status_file, "status"],
246+
stderr=subprocess.PIPE,
247+
**windows_flag,
243248
)
244249
errors = completed_process.stderr.decode()
245250
exit_status = completed_process.returncode
@@ -249,22 +254,25 @@ def pylsp_lint(
249254
exit_status,
250255
errors.strip(),
251256
)
252-
subprocess.run(["dmypy", "restart"], **windows_flag)
257+
subprocess.run(
258+
["dmypy", "--status-file", dmypy_status_file, "restart"], **windows_flag
259+
)
253260
else:
254261
# dmypy does not exist on path, but must exist in the env pylsp-mypy is installed in
255262
# -> use dmypy via api
256-
_, errors, exit_status = mypy_api.run_dmypy(["status"])
263+
_, errors, exit_status = mypy_api.run_dmypy(
264+
["--status-file", dmypy_status_file, "status"]
265+
)
257266
if exit_status != 0:
258267
log.info(
259268
"restarting dmypy from status: %s message: %s via api",
260269
exit_status,
261270
errors.strip(),
262271
)
263-
mypy_api.run_dmypy(["restart"])
272+
mypy_api.run_dmypy(["--status-file", dmypy_status_file, "restart"])
264273

265274
# run to use existing daemon or restart if required
266-
args = ["run", "--"] + apply_overrides(args, overrides)
267-
275+
args = ["--status-file", dmypy_status_file, "run", "--"] + apply_overrides(args, overrides)
268276
if shutil.which("dmypy"):
269277
# dmypy exists on path
270278
# -> use mypy on path

test/test_plugin.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ def test_option_overrides_dmypy(last_diagnostics_monkeypatch, workspace):
214214
)
215215
expected = [
216216
"dmypy",
217+
"--status-file",
218+
".dmypy.json",
217219
"run",
218220
"--",
219221
"--python-executable",
@@ -222,3 +224,32 @@ def test_option_overrides_dmypy(last_diagnostics_monkeypatch, workspace):
222224
document.path,
223225
]
224226
m.assert_called_with(expected, stderr=-1, stdout=-1, **windows_flag)
227+
228+
229+
def test_dmypy_status_file(tmpdir, last_diagnostics_monkeypatch, workspace):
230+
statusFile = tmpdir / ".custom_dmypy_status_file.json"
231+
232+
last_diagnostics_monkeypatch.setattr(
233+
FakeConfig,
234+
"plugin_settings",
235+
lambda _, p: {
236+
"dmypy": True,
237+
"live_mode": False,
238+
"dmypy_status_file": str(statusFile),
239+
}
240+
if p == "pylsp_mypy"
241+
else {},
242+
)
243+
244+
document = Document(DOC_URI, workspace, DOC_TYPE_ERR)
245+
246+
assert not statusFile.exists()
247+
248+
diags = plugin.pylsp_lint(
249+
config=FakeConfig(),
250+
workspace=workspace,
251+
document=document,
252+
is_saved=False,
253+
)
254+
255+
assert statusFile.exists()

0 commit comments

Comments
 (0)