Skip to content

Commit 78dee3d

Browse files
authored
Make flake8 use stdin (#830)
1 parent 2faf957 commit 78dee3d

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

pyls/plugins/flake8_lint.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ def pyls_lint(workspace, document):
4343
# Call the flake8 utility then parse diagnostics from stdout
4444
flake8_executable = settings.get('executable', 'flake8')
4545

46-
args = build_args(opts, document.path)
47-
output = run_flake8(flake8_executable, args)
46+
args = build_args(opts)
47+
output = run_flake8(flake8_executable, args, document)
4848
return parse_stdout(document, output)
4949

5050

51-
def run_flake8(flake8_executable, args):
51+
def run_flake8(flake8_executable, args, document):
5252
"""Run flake8 with the provided arguments, logs errors
5353
from stderr if any.
5454
"""
@@ -60,26 +60,25 @@ def run_flake8(flake8_executable, args):
6060
try:
6161
cmd = [flake8_executable]
6262
cmd.extend(args)
63-
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
63+
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
6464
except IOError:
6565
log.debug("Can't execute %s. Trying with 'python -m flake8'", flake8_executable)
6666
cmd = ['python', '-m', 'flake8']
6767
cmd.extend(args)
68-
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
69-
(stdout, stderr) = p.communicate()
68+
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
69+
(stdout, stderr) = p.communicate(document.source.encode())
7070
if stderr:
7171
log.error("Error while running flake8 '%s'", stderr.decode())
7272
return stdout.decode()
7373

7474

75-
def build_args(options, doc_path):
75+
def build_args(options):
7676
"""Build arguments for calling flake8.
7777
7878
Args:
7979
options: dictionary of argument names and their values.
80-
doc_path: path of the document to lint.
8180
"""
82-
args = [doc_path]
81+
args = ['-'] # use stdin
8382
for arg_name, arg_val in options.items():
8483
if arg_val is None:
8584
continue

test/plugins/test_flake8_lint.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ def temp_document(doc_text, workspace):
2828
return name, doc
2929

3030

31-
def test_flake8_no_checked_file(workspace):
32-
# A bad uri or a non-saved file may cause the flake8 linter to do nothing.
33-
# In this situtation, the linter will return an empty list.
34-
31+
def test_flake8_unsaved(workspace):
3532
doc = Document('', workspace, DOC)
3633
diags = flake8_lint.pyls_lint(workspace, doc)
37-
assert 'Error' in diags[0]['message']
34+
msg = 'local variable \'a\' is assigned to but never used'
35+
unused_var = [d for d in diags if d['message'] == msg][0]
36+
37+
assert unused_var['source'] == 'flake8'
38+
assert unused_var['code'] == 'F841'
39+
assert unused_var['range']['start'] == {'line': 5, 'character': 1}
40+
assert unused_var['range']['end'] == {'line': 5, 'character': 11}
41+
assert unused_var['severity'] == lsp.DiagnosticSeverity.Warning
3842

3943

4044
def test_flake8_lint(workspace):

0 commit comments

Comments
 (0)