Skip to content

Commit

Permalink
Add DiagnosticTag support (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski authored Jun 30, 2022
1 parent 5b29eac commit fa27a2b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 41 deletions.
44 changes: 26 additions & 18 deletions pylsp/plugins/flake8_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

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


@hookimpl
Expand Down Expand Up @@ -176,24 +183,25 @@ def parse_stdout(document, stdout):
severity = lsp.DiagnosticSeverity.Warning
if code == "E999" or code[0] == "F":
severity = lsp.DiagnosticSeverity.Error
diagnostics.append(
{
'source': 'flake8',
'code': code,
'range': {
'start': {
'line': line,
'character': character
},
'end': {
'line': line,
# no way to determine the column
'character': len(document.lines[line])
}
diagnostic = {
'source': 'flake8',
'code': code,
'range': {
'start': {
'line': line,
'character': character
},
'message': msg,
'severity': severity,
}
)
'end': {
'line': line,
# no way to determine the column
'character': len(document.lines[line])
}
},
'message': msg,
'severity': severity,
}
if code in UNNECESSITY_CODES:
diagnostic['tags'] = [lsp.DiagnosticTag.Unnecessary]
diagnostics.append(diagnostic)

return diagnostics
7 changes: 5 additions & 2 deletions pylsp/plugins/pycodestyle_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ def error(self, line_number, offset, text, check):
'character': 100 if line_number > len(self.lines) else len(self.lines[line_number - 1])
},
}
self.diagnostics.append({
diagnostic = {
'source': 'pycodestyle',
'range': err_range,
'message': text,
'code': code,
# Are style errors really ever errors?
'severity': _get_severity(code)
})
}
if code.startswith('W6'):
diagnostic['tags'] = [lsp.DiagnosticTag.Deprecated]
self.diagnostics.append(diagnostic)


def _get_severity(code):
Expand Down
68 changes: 47 additions & 21 deletions pylsp/plugins/pylint_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
# fix for a very specific upstream issue.
# Related: https://github.com/PyCQA/pylint/issues/3518
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'
DEPRECATION_CODES = {
'W0402', # Uses of a deprecated module %r
'W1505', # Using deprecated method %s()
'W1511', # Using deprecated argument %s of method %s()
'W1512', # Using deprecated class %s of module %s
'W1513', # Using deprecated decorator %s()
}
UNNECESSITY_CODES = {
'W0611', # Unused import %s
'W0612', # Unused variable %r
'W0613', # Unused argument %r
'W0614', # Unused import %s from wildcard import
'W1304', # Unused-format-string-argument
}


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

diagnostics.append({
code = diag['message-id']

diagnostic = {
'source': 'pylint',
'range': err_range,
'message': '[{}] {}'.format(diag['symbol'], diag['message']),
'severity': severity,
'code': diag['message-id']
})
'code': code
}

if code in UNNECESSITY_CODES:
diagnostic['tags'] = [lsp.DiagnosticTag.Unnecessary]
if code in DEPRECATION_CODES:
diagnostic['tags'] = [lsp.DiagnosticTag.Deprecated]

diagnostics.append(diagnostic)
cls.last_diags[document.path] = diagnostics
return diagnostics

Expand Down Expand Up @@ -295,24 +318,27 @@ def _parse_pylint_stdio_result(document, stdout):
'W': lsp.DiagnosticSeverity.Warning,
}
severity = severity_map[code[0]]
diagnostics.append(
{
'source': 'pylint',
'code': code,
'range': {
'start': {
'line': line,
'character': character
},
'end': {
'line': line,
# no way to determine the column
'character': len(document.lines[line]) - 1
}
diagnostic = {
'source': 'pylint',
'code': code,
'range': {
'start': {
'line': line,
'character': character
},
'message': msg,
'severity': severity,
}
)
'end': {
'line': line,
# no way to determine the column
'character': len(document.lines[line]) - 1
}
},
'message': msg,
'severity': severity,
}
if code in UNNECESSITY_CODES:
diagnostic['tags'] = [lsp.DiagnosticTag.Unnecessary]
if code in DEPRECATION_CODES:
diagnostic['tags'] = [lsp.DiagnosticTag.Deprecated]
diagnostics.append(diagnostic)

return diagnostics
1 change: 1 addition & 0 deletions test/plugins/test_flake8_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test_flake8_unsaved(workspace):
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['tags'] == [lsp.DiagnosticTag.Unnecessary]


def test_flake8_lint(workspace):
Expand Down
2 changes: 2 additions & 0 deletions test/plugins/test_pylint_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_pylint(config, workspace):

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

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

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

0 comments on commit fa27a2b

Please sign in to comment.