Skip to content

Commit 047af22

Browse files
committed
Make flake8 respect configuration
* ensure flake8 is run from the correct directory * make flake8 aware of the filename being linted
1 parent 659f6e5 commit 047af22

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

Diff for: pylsp/plugins/flake8_lint.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def run_flake8(flake8_executable, args, document):
9292
args = [(i if not i.startswith('--ignore=') else FIX_IGNORES_RE.sub('', i))
9393
for i in args if i is not None]
9494

95+
if document.path and document.path.startswith(document._workspace.root_path):
96+
args.extend(["--stdin-display-name", os.path.relpath(document.path, document._workspace.root_path)])
97+
9598
# if executable looks like a path resolve it
9699
if not os.path.isfile(flake8_executable) and os.sep in flake8_executable:
97100
flake8_executable = os.path.abspath(
@@ -102,12 +105,14 @@ def run_flake8(flake8_executable, args, document):
102105
try:
103106
cmd = [flake8_executable]
104107
cmd.extend(args)
105-
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
108+
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=document._workspace.root_path)
106109
except IOError:
107110
log.debug("Can't execute %s. Trying with '%s -m flake8'", flake8_executable, sys.executable)
108111
cmd = [sys.executable, '-m', 'flake8']
109112
cmd.extend(args)
110-
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) # pylint: disable=consider-using-with
113+
p = Popen( # pylint: disable=consider-using-with
114+
cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=document._workspace.root_path
115+
)
111116
(stdout, stderr) = p.communicate(document.source.encode())
112117
if stderr:
113118
log.error("Error while running flake8 '%s'", stderr.decode())

Diff for: test/plugins/test_flake8_lint.py

+61-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import tempfile
55
import os
6+
from textwrap import dedent
67
from unittest.mock import patch
78
from pylsp import lsp, uris
89
from pylsp.plugins import flake8_lint
@@ -59,6 +60,63 @@ def test_flake8_lint(workspace):
5960
os.remove(name)
6061

6162

63+
def test_flake8_respecting_configuration(workspace):
64+
docs = [
65+
("src/__init__.py", ""),
66+
("src/a.py", DOC),
67+
("src/b.py", "import os"),
68+
("setup.cfg", dedent("""
69+
[flake8]
70+
ignore = E302,W191
71+
per-file-ignores =
72+
src/a.py:F401
73+
src/b.py:W292
74+
"""))
75+
]
76+
77+
made = {}
78+
for rel, contents in docs:
79+
location = os.path.join(workspace.root_path, rel)
80+
made[rel] = {"uri": uris.from_fs_path(location)}
81+
82+
os.makedirs(os.path.dirname(location), exist_ok=True)
83+
with open(location, "w", encoding="utf-8") as fle:
84+
fle.write(contents)
85+
86+
workspace.put_document(made[rel]["uri"], contents)
87+
made[rel]["document"] = workspace._docs[made[rel]["uri"]]
88+
89+
diags = flake8_lint.pylsp_lint(workspace, made["src/a.py"]["document"])
90+
assert diags == [
91+
{
92+
"source": "flake8",
93+
"code": "F841",
94+
"range": {
95+
"start": {"line": 5, "character": 1},
96+
"end": {"line": 5, "character": 11},
97+
},
98+
"message": "F841 local variable 'a' is assigned to but never used",
99+
"severity": 1,
100+
"tags": [1],
101+
},
102+
]
103+
104+
diags = flake8_lint.pylsp_lint(workspace, made["src/b.py"]["document"])
105+
assert diags == [
106+
{
107+
"source": "flake8",
108+
"code": "F401",
109+
"range": {
110+
"start": {"line": 0, "character": 0},
111+
"end": {"line": 0, "character": 9},
112+
},
113+
"message": "F401 'os' imported but unused",
114+
"severity": 1,
115+
"tags": [1],
116+
}
117+
]
118+
119+
62120
def test_flake8_config_param(workspace):
63121
with patch('pylsp.plugins.flake8_lint.Popen') as popen_mock:
64122
mock_instance = popen_mock.return_value
@@ -126,7 +184,9 @@ def test_flake8_multiline(workspace):
126184
flake8_lint.pylsp_lint(workspace, doc)
127185

128186
call_args = popen_mock.call_args[0][0]
129-
assert call_args == ["flake8", "-", "--exclude=blah/,file_2.py"]
187+
188+
init_file = os.path.join("blah", "__init__.py")
189+
assert call_args == ["flake8", "-", "--exclude=blah/,file_2.py", "--stdin-display-name", init_file]
130190

131191
os.unlink(os.path.join(workspace.root_path, "setup.cfg"))
132192

0 commit comments

Comments
 (0)