Skip to content

Commit c83a95d

Browse files
authored
Merge branch 'master' into overrides
2 parents ce3844f + 07a5bc2 commit c83a95d

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

pylsp_mypy/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.5.4"
1+
__version__ = "0.5.6"

pylsp_mypy/plugin.py

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
import subprocess
1414
from pathlib import Path
1515
import logging
16+
from mypy import api as mypy_api
1617
from pylsp import hookimpl
1718
from pylsp.workspace import Document, Workspace
1819
from pylsp.config.config import Config
1920
from typing import Optional, Dict, Any, IO, List
2021
import atexit
2122
import collections
2223
import warnings
24+
import shutil
25+
import ast
2326

2427
line_pattern: str = r"((?:^[a-z]:)?[^:]+):(?:(\d+):)?(?:(\d+):)? (\w+): (.*)"
2528

@@ -204,35 +207,65 @@ def pylsp_lint(
204207
args.extend(["--incremental", "--follow-imports", "silent"])
205208
args = apply_overrides(args, overrides)
206209

207-
log.info("executing mypy args = %s", args)
208-
completed_process = subprocess.run(
209-
["mypy", *args], stdout=subprocess.PIPE, stderr=subprocess.PIPE
210-
)
211-
report = completed_process.stdout.decode()
212-
errors = completed_process.stderr.decode()
210+
if shutil.which("mypy"):
211+
# mypy exists on path
212+
# -> use mypy on path
213+
log.info("executing mypy args = %s on path", args)
214+
completed_process = subprocess.run(
215+
["mypy", *args], stdout=subprocess.PIPE, stderr=subprocess.PIPE
216+
)
217+
report = completed_process.stdout.decode()
218+
errors = completed_process.stderr.decode()
219+
else:
220+
# mypy does not exist on path, but must exist in the env pylsp-mypy is installed in
221+
# -> use mypy via api
222+
log.info("executing mypy args = %s via api", args)
223+
report, errors, _ = mypy_api.run(args)
213224
else:
214225
# If dmypy daemon is non-responsive calls to run will block.
215226
# Check daemon status, if non-zero daemon is dead or hung.
216227
# If daemon is hung, kill will reset
217228
# If daemon is dead/absent, kill will no-op.
218229
# In either case, reset to fresh state
219-
completed_process = subprocess.run(
220-
["dmypy", *apply_overrides(args, overrides)], stderr=subprocess.PIPE
221-
)
222-
_err = completed_process.stderr.decode()
223-
_status = completed_process.returncode
224-
if _status != 0:
225-
log.info("restarting dmypy from status: %s message: %s", _status, _err.strip())
226-
subprocess.run(["dmypy", "kill"])
230+
231+
if shutil.which("dmypy"):
232+
# dmypy exists on path
233+
# -> use mypy on path
234+
completed_process = subprocess.run(["dmypy", *apply_overrides(args, overrides)], stderr=subprocess.PIPE)
235+
_err = completed_process.stderr.decode()
236+
_status = completed_process.returncode
237+
if _status != 0:
238+
log.info(
239+
"restarting dmypy from status: %s message: %s via path", _status, _err.strip()
240+
)
241+
subprocess.run(["dmypy", "kill"])
242+
else:
243+
# dmypy does not exist on path, but must exist in the env pylsp-mypy is installed in
244+
# -> use dmypy via api
245+
_, _err, _status = mypy_api.run_dmypy(["status"])
246+
if _status != 0:
247+
log.info(
248+
"restarting dmypy from status: %s message: %s via api", _status, _err.strip()
249+
)
250+
mypy_api.run_dmypy(["kill"])
227251

228252
# run to use existing daemon or restart if required
229253
args = ["run", "--"] + apply_overrides(args, overrides)
230-
log.info("dmypy run args = %s", args)
231-
completed_process = subprocess.run(
232-
["dmypy", *args], stdout=subprocess.PIPE, stderr=subprocess.PIPE
233-
)
234-
report = completed_process.stdout.decode()
235-
errors = completed_process.stderr.decode()
254+
255+
if shutil.which("dmypy"):
256+
# dmypy exists on path
257+
# -> use mypy on path
258+
log.info("dmypy run args = %s via path", args)
259+
completed_process = subprocess.run(
260+
["dmypy", *args], stdout=subprocess.PIPE, stderr=subprocess.PIPE
261+
)
262+
report = completed_process.stdout.decode()
263+
errors = completed_process.stderr.decode()
264+
else:
265+
# dmypy does not exist on path, but must exist in the env pylsp-mypy is installed in
266+
# -> use dmypy via api
267+
log.info("dmypy run args = %s via api", args)
268+
report, errors, _ = mypy_api.run_dmypy(args)
236269

237270
log.debug("report:\n%s", report)
238271
log.debug("errors:\n%s", errors)
@@ -291,7 +324,7 @@ def init(workspace: str) -> Dict[str, str]:
291324
path = findConfigFile(workspace, ["pylsp-mypy.cfg", "mypy-ls.cfg", "mypy_ls.cfg"])
292325
if path:
293326
with open(path) as file:
294-
configuration = eval(file.read())
327+
configuration = ast.literal_eval(file.read())
295328

296329
mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini"])
297330
mypyConfigFileMap[workspace] = mypyConfigFile

test/test_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def foo():
110110

111111
# Create configuration file for workspace folder 1.
112112
mypy_config = folder1.join("mypy.ini")
113-
mypy_config.write("[mypy]\nwarn_unreachable = True")
113+
mypy_config.write("[mypy]\nwarn_unreachable = True\ncheck_untyped_defs = True")
114114

115115
# Initialize settings for both folders.
116116
plugin.pylsp_settings(ws1._config)

0 commit comments

Comments
 (0)