From 03975b7710ab8df20a3166b2a3000dafc561347b Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Fri, 16 Sep 2022 20:06:24 +0200 Subject: [PATCH 1/5] Expose generic mypy error as diagnostic --- .github/workflows/python-package.yml | 4 ++-- pylsp_mypy/plugin.py | 16 ++++++++++++++++ setup.cfg | 3 +++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 2bf7b1f..fe48ca1 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -38,8 +38,8 @@ jobs: 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 + # exit-zero treats all errors as warnings + flake8 . --count --exit-zero --statistics - name: Check black formatting run: | # stop the build if black detect any changes diff --git a/pylsp_mypy/plugin.py b/pylsp_mypy/plugin.py index 5d037f2..7934dce 100644 --- a/pylsp_mypy/plugin.py +++ b/pylsp_mypy/plugin.py @@ -278,6 +278,22 @@ def pylsp_lint( 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, # Error + } + ) + for line in report.splitlines(): log.debug("parsing: line = %r", line) diag = parse_line(line, document) diff --git a/setup.cfg b/setup.cfg index b4ecef4..85b0498 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,6 +23,9 @@ install_requires = mypy toml +[flake8] +max-complexity = 10 +max-line-length = 127 [options.entry_points] pylsp = pylsp_mypy = pylsp_mypy.plugin From 2e5bfb953fdca7b2df3b3a4b6c7211f7cb0fb9f2 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Thu, 22 Sep 2022 17:26:28 +0200 Subject: [PATCH 2/5] create error or warning depending on process return code --- pylsp_mypy/plugin.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/pylsp_mypy/plugin.py b/pylsp_mypy/plugin.py index 7934dce..0e18d55 100644 --- a/pylsp_mypy/plugin.py +++ b/pylsp_mypy/plugin.py @@ -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"]) @@ -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. @@ -239,20 +241,20 @@ 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"]) @@ -268,11 +270,12 @@ 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) @@ -290,7 +293,7 @@ def pylsp_lint( "end": {"line": 0, "character": 1000}, }, "message": errors, - "severity": 1, # Error + "severity": 1 if exit_status != 0 else 2, # Error if exited with error or warning. } ) From ae88f1d3c44843e7eefab16b977b2a3c785ec560 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Thu, 22 Sep 2022 17:30:06 +0200 Subject: [PATCH 3/5] black reformat --- pylsp_mypy/plugin.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pylsp_mypy/plugin.py b/pylsp_mypy/plugin.py index 0e18d55..7ad35f4 100644 --- a/pylsp_mypy/plugin.py +++ b/pylsp_mypy/plugin.py @@ -245,7 +245,9 @@ def pylsp_lint( exit_status = completed_process.returncode if exit_status != 0: log.info( - "restarting dmypy from status: %s message: %s via path", exit_status, errors.strip() + "restarting dmypy from status: %s message: %s via path", + exit_status, + errors.strip(), ) subprocess.run(["dmypy", "kill"], **windows_flag) else: @@ -254,7 +256,9 @@ def pylsp_lint( _, errors, exit_status = mypy_api.run_dmypy(["status"]) if exit_status != 0: log.info( - "restarting dmypy from status: %s message: %s via api", exit_status, errors.strip() + "restarting dmypy from status: %s message: %s via api", + exit_status, + errors.strip(), ) mypy_api.run_dmypy(["kill"]) From d3b86abddb1c76cf2baf45c914c06b5d4e75a492 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Thu, 22 Sep 2022 17:48:20 +0200 Subject: [PATCH 4/5] 100 column limit --- setup.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index 85b0498..3bbe0db 100644 --- a/setup.cfg +++ b/setup.cfg @@ -24,8 +24,8 @@ install_requires = toml [flake8] -max-complexity = 10 -max-line-length = 127 +max-complexity = 20 +max-line-length = 100 [options.entry_points] pylsp = pylsp_mypy = pylsp_mypy.plugin From 20db63a351de4dd5c519c5aff57b8c799aed2b3a Mon Sep 17 00:00:00 2001 From: Richard Kellnberger Date: Sun, 9 Oct 2022 20:46:28 +0200 Subject: [PATCH 5/5] Detect all errors --- .github/workflows/python-package.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index fe48ca1..0ca9e3a 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -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 - flake8 . --count --exit-zero --statistics + flake8 . --count --show-source --statistics - name: Check black formatting run: | # stop the build if black detect any changes