Skip to content

Commit f810710

Browse files
authored
add pattern matching to severities (#66)
1 parent 8a0270c commit f810710

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,4 @@ This default can be changed through the `pylsp.plugins.ruff.severities` option,
9494
For more information on the diagnostic severities please refer to
9595
[the official LSP reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnosticSeverity).
9696

97-
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,
98-
please check first that the error code is correct and that the given value is one of the possible keys from above.
97+
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.

pylsp_ruff/plugin.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,18 @@ def create_diagnostic(check: RuffCheck, settings: PluginSettings) -> Diagnostic:
207207
if check.code == "E999" or check.code[0] == "F":
208208
severity = DiagnosticSeverity.Error
209209

210-
# Override severity with custom severity if possible, use default otherwise
210+
# Check if check.code starts contained in given severities
211211
if settings.severities is not None:
212-
custom_sev = settings.severities.get(check.code, None)
213-
if custom_sev is not None:
212+
_custom_sev = [
213+
sev
214+
for pat, sev in sorted(
215+
settings.severities.items(), key=lambda key: (len(key), key)
216+
)
217+
if check.code.startswith(pat)
218+
]
219+
220+
if _custom_sev:
221+
custom_sev = _custom_sev[-1]
214222
severity = DIAGNOSTIC_SEVERITIES.get(custom_sev, severity)
215223

216224
tags = []

tests/test_ruff_lint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def f():
195195
"plugins": {
196196
"ruff": {
197197
"extendIgnore": ["D104"],
198-
"severities": {"E402": "E", "D103": "I"},
198+
"severities": {"E402": "E", "D": "I", "D1": "H"},
199199
}
200200
}
201201
}
@@ -217,7 +217,7 @@ def f():
217217
if diag["code"] == "E402":
218218
assert diag["severity"] == 1
219219
if diag["code"] == "D103":
220-
assert diag["severity"] == 3
220+
assert diag["severity"] == 4 # Should take "D1" over "D"
221221

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

0 commit comments

Comments
 (0)