Skip to content

Commit

Permalink
Merge pull request #40 from rchl/fix/generic-error
Browse files Browse the repository at this point in the history
Expose generic mypy error as a diagnostic
  • Loading branch information
Richardk2n authored Oct 9, 2022
2 parents 5c1bd61 + 20db63a commit f12354d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ jobs:
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
flake8 . --count --show-source --statistics
- name: Check black formatting
run: |
# stop the build if black detect any changes
Expand Down
41 changes: 32 additions & 9 deletions pylsp_mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def pylsp_lint(
args.append("--strict")

overrides = settings.get("overrides", [True])
exit_status = 0

if not dmypy:
args.extend(["--incremental", "--follow-imports", "silent"])
Expand All @@ -221,11 +222,12 @@ def pylsp_lint(
)
report = completed_process.stdout.decode()
errors = completed_process.stderr.decode()
exit_status = completed_process.returncode
else:
# mypy does not exist on path, but must exist in the env pylsp-mypy is installed in
# -> use mypy via api
log.info("executing mypy args = %s via api", args)
report, errors, _ = mypy_api.run(args)
report, errors, exit_status = mypy_api.run(args)
else:
# If dmypy daemon is non-responsive calls to run will block.
# Check daemon status, if non-zero daemon is dead or hung.
Expand All @@ -239,20 +241,24 @@ def pylsp_lint(
completed_process = subprocess.run(
["dmypy", *apply_overrides(args, overrides)], stderr=subprocess.PIPE, **windows_flag
)
_err = completed_process.stderr.decode()
_status = completed_process.returncode
if _status != 0:
errors = completed_process.stderr.decode()
exit_status = completed_process.returncode
if exit_status != 0:
log.info(
"restarting dmypy from status: %s message: %s via path", _status, _err.strip()
"restarting dmypy from status: %s message: %s via path",
exit_status,
errors.strip(),
)
subprocess.run(["dmypy", "kill"], **windows_flag)
else:
# dmypy does not exist on path, but must exist in the env pylsp-mypy is installed in
# -> use dmypy via api
_, _err, _status = mypy_api.run_dmypy(["status"])
if _status != 0:
_, errors, exit_status = mypy_api.run_dmypy(["status"])
if exit_status != 0:
log.info(
"restarting dmypy from status: %s message: %s via api", _status, _err.strip()
"restarting dmypy from status: %s message: %s via api",
exit_status,
errors.strip(),
)
mypy_api.run_dmypy(["kill"])

Expand All @@ -268,16 +274,33 @@ def pylsp_lint(
)
report = completed_process.stdout.decode()
errors = completed_process.stderr.decode()
exit_status = completed_process.returncode
else:
# dmypy does not exist on path, but must exist in the env pylsp-mypy is installed in
# -> use dmypy via api
log.info("dmypy run args = %s via api", args)
report, errors, _ = mypy_api.run_dmypy(args)
report, errors, exit_status = mypy_api.run_dmypy(args)

log.debug("report:\n%s", report)
log.debug("errors:\n%s", errors)

diagnostics = []

# Expose generic mypy error on the first line.
if errors:
diagnostics.append(
{
"source": "mypy",
"range": {
"start": {"line": 0, "character": 0},
# Client is supposed to clip end column to line length.
"end": {"line": 0, "character": 1000},
},
"message": errors,
"severity": 1 if exit_status != 0 else 2, # Error if exited with error or warning.
}
)

for line in report.splitlines():
log.debug("parsing: line = %r", line)
diag = parse_line(line, document)
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ install_requires =
mypy
toml

[flake8]
max-complexity = 20
max-line-length = 100

[options.entry_points]
pylsp = pylsp_mypy = pylsp_mypy.plugin
Expand Down

0 comments on commit f12354d

Please sign in to comment.