Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine diagnostic severity for flake8 #490

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion pylsp/plugins/flake8_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@
"F523", # .format(...) unused positional arguments
"F841", # local variable `name` is assigned to but never used
}
ERROR_CODES = {
# Errors from flake8 itself
"E999", # syntax error
# Errors from the pyflakes plugin of flake8
#
# The following list should be kept in sync with the `pyflakes_lint` plugin.
# Here is a snippet to generate such a list:
#
# from flake8.plugins.pyflakes import FLAKE8_PYFLAKES_CODES
# from pylsp.plugins.pyflakes_lint import PYFLAKES_ERROR_MESSAGES
# for m in PYFLAKES_ERROR_MESSAGES:
# print(f'"{FLAKE8_PYFLAKES_CODES[m.__name__]}", # {m.__name__}')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to use something similar to this code to keep Pyflakes and Flake8 error codes in sync without manual maintenance.

In other words, I suggest to build ERROR_CODES like this (with imports in the appropriate places, of course):

from flake8.plugins.pyflakes import FLAKE8_PYFLAKES_CODES

from pylsp.plugins.pyflakes_lint import PYFLAKES_ERROR_MESSAGES

ERROR_CODES = {FLAKE8_PYFLAKES_CODES[m.__name__] for m in PYFLAKES_ERROR_MESSAGES} | {"E999"}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

I was considering doing so but decided to maintain it manually because:

  • Flake8 does not guarantee a stable API, so FLAKE8_PYFLAKES_CODES is somewhat internal.
  • We run flake8 as an executable and parse its outputs, so using the flake8 module may bring import errors or inconsistencies (e.g., the user installs Flake8 in a different virtual environment from pylsp).

Honestly both are quite subtle though. If you think they are not problems I will change to what you suggest. Thank you!

"F821", # UndefinedName
"F822", # UndefinedExport
"F823", # UndefinedLocal
"F831", # DuplicateArgument
"F407", # FutureFeatureNotDefined
"F706", # ReturnOutsideFunction
"F704", # YieldOutsideFunction
"F702", # ContinueOutsideLoop
"F701", # BreakOutsideLoop
"F622", # TwoStarredExpressions
}


@hookimpl
Expand Down Expand Up @@ -208,7 +231,7 @@ def parse_stdout(source, stdout):
# show also the code in message
msg = code + " " + msg
severity = lsp.DiagnosticSeverity.Warning
if code == "E999" or code[0] == "F":
if code in ERROR_CODES:
severity = lsp.DiagnosticSeverity.Error
diagnostic = {
"source": "flake8",
Expand Down
8 changes: 4 additions & 4 deletions test/plugins/test_flake8_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_flake8_unsaved(workspace):
assert unused_var["code"] == "F841"
assert unused_var["range"]["start"] == {"line": 5, "character": 1}
assert unused_var["range"]["end"] == {"line": 5, "character": 11}
assert unused_var["severity"] == lsp.DiagnosticSeverity.Error
assert unused_var["severity"] == lsp.DiagnosticSeverity.Warning
assert unused_var["tags"] == [lsp.DiagnosticTag.Unnecessary]


Expand All @@ -55,7 +55,7 @@ def test_flake8_lint(workspace):
assert unused_var["code"] == "F841"
assert unused_var["range"]["start"] == {"line": 5, "character": 1}
assert unused_var["range"]["end"] == {"line": 5, "character": 11}
assert unused_var["severity"] == lsp.DiagnosticSeverity.Error
assert unused_var["severity"] == lsp.DiagnosticSeverity.Warning
finally:
os.remove(name)

Expand Down Expand Up @@ -101,7 +101,7 @@ def test_flake8_respecting_configuration(workspace):
"end": {"line": 5, "character": 11},
},
"message": "F841 local variable 'a' is assigned to but never used",
"severity": 1,
"severity": 2,
"tags": [1],
},
]
Expand All @@ -116,7 +116,7 @@ def test_flake8_respecting_configuration(workspace):
"end": {"line": 0, "character": 9},
},
"message": "F401 'os' imported but unused",
"severity": 1,
"severity": 2,
"tags": [1],
}
]
Expand Down
Loading