Skip to content

Commit fa27a2b

Browse files
authored
Add DiagnosticTag support (#229)
1 parent 5b29eac commit fa27a2b

File tree

5 files changed

+81
-41
lines changed

5 files changed

+81
-41
lines changed

pylsp/plugins/flake8_lint.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313

1414
log = logging.getLogger(__name__)
1515
FIX_IGNORES_RE = re.compile(r'([^a-zA-Z0-9_,]*;.*(\W+||$))')
16+
UNNECESSITY_CODES = {
17+
'F401', # `module` imported but unused
18+
'F504', # % format unused named arguments
19+
'F522', # .format(...) unused named arguments
20+
'F523', # .format(...) unused positional arguments
21+
'F841' # local variable `name` is assigned to but never used
22+
}
1623

1724

1825
@hookimpl
@@ -176,24 +183,25 @@ def parse_stdout(document, stdout):
176183
severity = lsp.DiagnosticSeverity.Warning
177184
if code == "E999" or code[0] == "F":
178185
severity = lsp.DiagnosticSeverity.Error
179-
diagnostics.append(
180-
{
181-
'source': 'flake8',
182-
'code': code,
183-
'range': {
184-
'start': {
185-
'line': line,
186-
'character': character
187-
},
188-
'end': {
189-
'line': line,
190-
# no way to determine the column
191-
'character': len(document.lines[line])
192-
}
186+
diagnostic = {
187+
'source': 'flake8',
188+
'code': code,
189+
'range': {
190+
'start': {
191+
'line': line,
192+
'character': character
193193
},
194-
'message': msg,
195-
'severity': severity,
196-
}
197-
)
194+
'end': {
195+
'line': line,
196+
# no way to determine the column
197+
'character': len(document.lines[line])
198+
}
199+
},
200+
'message': msg,
201+
'severity': severity,
202+
}
203+
if code in UNNECESSITY_CODES:
204+
diagnostic['tags'] = [lsp.DiagnosticTag.Unnecessary]
205+
diagnostics.append(diagnostic)
198206

199207
return diagnostics

pylsp/plugins/pycodestyle_lint.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,17 @@ def error(self, line_number, offset, text, check):
7575
'character': 100 if line_number > len(self.lines) else len(self.lines[line_number - 1])
7676
},
7777
}
78-
self.diagnostics.append({
78+
diagnostic = {
7979
'source': 'pycodestyle',
8080
'range': err_range,
8181
'message': text,
8282
'code': code,
8383
# Are style errors really ever errors?
8484
'severity': _get_severity(code)
85-
})
85+
}
86+
if code.startswith('W6'):
87+
diagnostic['tags'] = [lsp.DiagnosticTag.Deprecated]
88+
self.diagnostics.append(diagnostic)
8689

8790

8891
def _get_severity(code):

pylsp/plugins/pylint_lint.py

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@
2828
# fix for a very specific upstream issue.
2929
# Related: https://github.com/PyCQA/pylint/issues/3518
3030
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'
31+
DEPRECATION_CODES = {
32+
'W0402', # Uses of a deprecated module %r
33+
'W1505', # Using deprecated method %s()
34+
'W1511', # Using deprecated argument %s of method %s()
35+
'W1512', # Using deprecated class %s of module %s
36+
'W1513', # Using deprecated decorator %s()
37+
}
38+
UNNECESSITY_CODES = {
39+
'W0611', # Unused import %s
40+
'W0612', # Unused variable %r
41+
'W0613', # Unused argument %r
42+
'W0614', # Unused import %s from wildcard import
43+
'W1304', # Unused-format-string-argument
44+
}
3145

3246

3347
class PylintLinter:
@@ -146,13 +160,22 @@ def lint(cls, document, is_saved, flags=''):
146160
elif diag['type'] == 'warning':
147161
severity = lsp.DiagnosticSeverity.Warning
148162

149-
diagnostics.append({
163+
code = diag['message-id']
164+
165+
diagnostic = {
150166
'source': 'pylint',
151167
'range': err_range,
152168
'message': '[{}] {}'.format(diag['symbol'], diag['message']),
153169
'severity': severity,
154-
'code': diag['message-id']
155-
})
170+
'code': code
171+
}
172+
173+
if code in UNNECESSITY_CODES:
174+
diagnostic['tags'] = [lsp.DiagnosticTag.Unnecessary]
175+
if code in DEPRECATION_CODES:
176+
diagnostic['tags'] = [lsp.DiagnosticTag.Deprecated]
177+
178+
diagnostics.append(diagnostic)
156179
cls.last_diags[document.path] = diagnostics
157180
return diagnostics
158181

@@ -295,24 +318,27 @@ def _parse_pylint_stdio_result(document, stdout):
295318
'W': lsp.DiagnosticSeverity.Warning,
296319
}
297320
severity = severity_map[code[0]]
298-
diagnostics.append(
299-
{
300-
'source': 'pylint',
301-
'code': code,
302-
'range': {
303-
'start': {
304-
'line': line,
305-
'character': character
306-
},
307-
'end': {
308-
'line': line,
309-
# no way to determine the column
310-
'character': len(document.lines[line]) - 1
311-
}
321+
diagnostic = {
322+
'source': 'pylint',
323+
'code': code,
324+
'range': {
325+
'start': {
326+
'line': line,
327+
'character': character
312328
},
313-
'message': msg,
314-
'severity': severity,
315-
}
316-
)
329+
'end': {
330+
'line': line,
331+
# no way to determine the column
332+
'character': len(document.lines[line]) - 1
333+
}
334+
},
335+
'message': msg,
336+
'severity': severity,
337+
}
338+
if code in UNNECESSITY_CODES:
339+
diagnostic['tags'] = [lsp.DiagnosticTag.Unnecessary]
340+
if code in DEPRECATION_CODES:
341+
diagnostic['tags'] = [lsp.DiagnosticTag.Deprecated]
342+
diagnostics.append(diagnostic)
317343

318344
return diagnostics

test/plugins/test_flake8_lint.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def test_flake8_unsaved(workspace):
4040
assert unused_var['range']['start'] == {'line': 5, 'character': 1}
4141
assert unused_var['range']['end'] == {'line': 5, 'character': 11}
4242
assert unused_var['severity'] == lsp.DiagnosticSeverity.Error
43+
assert unused_var['tags'] == [lsp.DiagnosticTag.Unnecessary]
4344

4445

4546
def test_flake8_lint(workspace):

test/plugins/test_pylint_lint.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def test_pylint(config, workspace):
5151

5252
assert unused_import['range']['start'] == {'line': 0, 'character': 0}
5353
assert unused_import['severity'] == lsp.DiagnosticSeverity.Warning
54+
assert unused_import['tags'] == [lsp.DiagnosticTag.Unnecessary]
5455

5556
if IS_PY3:
5657
# test running pylint in stdin
@@ -79,6 +80,7 @@ def test_syntax_error_pylint_py3(config, workspace):
7980
# Pylint doesn't give column numbers for invalid syntax.
8081
assert diag['range']['start'] == {'line': 0, 'character': 12}
8182
assert diag['severity'] == lsp.DiagnosticSeverity.Error
83+
assert 'tags' not in diag
8284

8385
# test running pylint in stdin
8486
config.plugin_settings('pylint')['executable'] = 'pylint'

0 commit comments

Comments
 (0)