Skip to content

Commit 738730a

Browse files
authored
Mark some pyflakes flakes as errors (#297)
* Mark some pyflakes flakes as errors * linting
1 parent 551f72d commit 738730a

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

pyls/plugins/pyflakes_lint.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
# Copyright 2017 Palantir Technologies, Inc.
2-
from pyflakes import api as pyflakes_api
2+
from pyflakes import api as pyflakes_api, messages
33
from pyls import hookimpl, lsp
44

5+
# Pyflakes messages that should be reported as Errors instead of Warns
6+
PYFLAKES_ERROR_MESSAGES = (
7+
messages.UndefinedName,
8+
messages.UndefinedExport,
9+
messages.UndefinedLocal,
10+
messages.DuplicateArgument,
11+
messages.FutureFeatureNotDefined,
12+
messages.ReturnOutsideFunction,
13+
messages.YieldOutsideFunction,
14+
messages.ContinueOutsideLoop,
15+
messages.BreakOutsideLoop,
16+
messages.ContinueInFinally,
17+
messages.TwoStarredExpressions,
18+
)
19+
520

621
@hookimpl
722
def pyls_lint(document):
@@ -37,9 +52,16 @@ def flake(self, message):
3752
'start': {'line': message.lineno - 1, 'character': message.col},
3853
'end': {'line': message.lineno - 1, 'character': len(self.lines[message.lineno - 1])},
3954
}
55+
56+
severity = lsp.DiagnosticSeverity.Warning
57+
for message_type in PYFLAKES_ERROR_MESSAGES:
58+
if isinstance(message, message_type):
59+
severity = lsp.DiagnosticSeverity.Error
60+
break
61+
4062
self.diagnostics.append({
4163
'source': 'pyflakes',
4264
'range': err_range,
4365
'message': message.message % message.message_args,
44-
'severity': lsp.DiagnosticSeverity.Warning
66+
'severity': severity
4567
})

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
'mccabe',
4141
'pycodestyle',
4242
'pydocstyle>=2.0.0',
43-
'pyflakes',
43+
'pyflakes>=1.6.0',
4444
'rope>=0.10.5',
4545
'yapf',
4646
'pluggy'

test/plugins/test_pyflakes_lint.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright 2017 Palantir Technologies, Inc.
2-
from pyls import uris
2+
from pyls import lsp, uris
33
from pyls.workspace import Document
44
from pyls.plugins import pyflakes_lint
55

@@ -16,6 +16,8 @@ def hello():
1616
pass
1717
"""
1818

19+
DOC_UNDEFINED_NAME_ERR = "a = b"
20+
1921

2022
def test_pyflakes():
2123
doc = Document(DOC_URI, DOC)
@@ -26,6 +28,7 @@ def test_pyflakes():
2628
unused_import = [d for d in diags if d['message'] == msg][0]
2729

2830
assert unused_import['range']['start'] == {'line': 0, 'character': 0}
31+
assert unused_import['severity'] == lsp.DiagnosticSeverity.Warning
2932

3033

3134
def test_syntax_error_pyflakes():
@@ -34,3 +37,13 @@ def test_syntax_error_pyflakes():
3437

3538
assert diag['message'] == 'invalid syntax'
3639
assert diag['range']['start'] == {'line': 0, 'character': 12}
40+
assert diag['severity'] == lsp.DiagnosticSeverity.Error
41+
42+
43+
def test_undefined_name_pyflakes():
44+
doc = Document(DOC_URI, DOC_UNDEFINED_NAME_ERR)
45+
diag = pyflakes_lint.pyls_lint(doc)[0]
46+
47+
assert diag['message'] == 'undefined name \'b\''
48+
assert diag['range']['start'] == {'line': 0, 'character': 4}
49+
assert diag['severity'] == lsp.DiagnosticSeverity.Error

0 commit comments

Comments
 (0)