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

Add pattern matching to severities #66

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,4 @@ This default can be changed through the `pylsp.plugins.ruff.severities` option,
For more information on the diagnostic severities please refer to
[the official LSP reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnosticSeverity).

Note that `python-lsp-ruff` does *not* accept regex, and it will *not* check whether the error code exists. If the custom severity level is not displayed,
please check first that the error code is correct and that the given value is one of the possible keys from above.
With `v2.0.0` it is also possible to use patterns to match codes. Rules match if the error code starts with the given pattern. If multiple patterns match the error code, `python-lsp-ruff` chooses the one with the most amount of matching characters.
14 changes: 11 additions & 3 deletions pylsp_ruff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,18 @@ def create_diagnostic(check: RuffCheck, settings: PluginSettings) -> Diagnostic:
if check.code == "E999" or check.code[0] == "F":
severity = DiagnosticSeverity.Error

# Override severity with custom severity if possible, use default otherwise
# Check if check.code starts contained in given severities
if settings.severities is not None:
custom_sev = settings.severities.get(check.code, None)
if custom_sev is not None:
_custom_sev = [
sev
for pat, sev in sorted(
settings.severities.items(), key=lambda key: (len(key), key)
)
if check.code.startswith(pat)
]

if _custom_sev:
custom_sev = _custom_sev[-1]
severity = DIAGNOSTIC_SEVERITIES.get(custom_sev, severity)

tags = []
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ruff_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def f():
"plugins": {
"ruff": {
"extendIgnore": ["D104"],
"severities": {"E402": "E", "D103": "I"},
"severities": {"E402": "E", "D": "I", "D1": "H"},
}
}
}
Expand All @@ -217,7 +217,7 @@ def f():
if diag["code"] == "E402":
assert diag["severity"] == 1
if diag["code"] == "D103":
assert diag["severity"] == 3
assert diag["severity"] == 4 # Should take "D1" over "D"

# Excludes
doc_uri = uris.from_fs_path(os.path.join(workspace.root_path, "blah/__init__.py"))
Expand Down