Skip to content

Commit ff02a39

Browse files
authored
Merge pull request #6 from python-lsp/fix_#5
Fix `per-file-ignores`
2 parents 1aea798 + a37cb4c commit ff02a39

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

pylsp_ruff/ruff_lint.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def run_ruff_lint(ruff_executable: str, document: Document, arguments: list) ->
9393
# Ruff doesn't yet support calling with python -m ruff,
9494
# see https://github.com/charliermarsh/ruff/issues/593
9595
log.error(f"Error running {ruff_executable}: {e}")
96-
9796
(stdout, stderr) = p.communicate(document.source.encode())
9897

9998
if stderr:
@@ -179,6 +178,11 @@ def build_args(document: Document, options: dict) -> list:
179178
args.extend(["--format=json"])
180179
# Do not attempt to fix -> returns file instead of diagnostics
181180
args.extend(["--no-fix"])
181+
# Always force excludes
182+
args.extend(["--force-exclude"])
183+
# Pass filename to ruff for per-file-ignores, catch unsaved
184+
if document.path != "":
185+
args.extend(["--stdin-filename", document.path])
182186

183187
# Convert per-file-ignores dict to right format
184188
per_file_ignores = options.pop("per-file-ignores")

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ name = "python-lsp-ruff"
77
authors = [
88
{name = "Julian Hossbach", email = "[email protected]"}
99
]
10-
version = "1.0.2"
10+
version = "1.0.3"
1111
description = "Ruff linting plugin for pylsp"
1212
readme = "README.md"
1313
requires-python = ">=3.7"
1414
license = {text = "MIT"}
1515
dependencies = [
16-
"ruff>=0.0.106",
16+
"ruff>=0.0.192",
1717
"python-lsp-server",
1818
]
1919

tests/test_ruff_lint.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def temp_document(doc_text, workspace):
3838
name = temp_file.name
3939
temp_file.write(doc_text)
4040
doc = Document(uris.from_fs_path(name), workspace)
41-
4241
return name, doc
4342

4443

@@ -118,21 +117,24 @@ def get_ruff_cfg_settings(workspace, doc, config_str):
118117

119118
def test_ruff_config(workspace):
120119
config_str = r"""[tool.ruff]
121-
ignore = ["F481"]
120+
ignore = ["F841"]
122121
exclude = [
123-
"blah",
122+
"blah/__init__.py",
124123
"file_2.py"
125124
]
126125
[tool.ruff.per-file-ignores]
127-
"__init__.py" = ["F401", "E402"]
128-
"test_something.py" = ["E402"]
129-
"""
126+
"test_something.py" = ["F401"]
127+
"""
130128

131-
doc_str = "print('hi')\nimport os\n"
129+
doc_str = r"""
130+
print('hi')
131+
import os
132+
def f():
133+
a = 2
134+
"""
132135

133-
doc_uri = uris.from_fs_path(os.path.join(workspace.root_path, "blah/__init__.py"))
136+
doc_uri = uris.from_fs_path(os.path.join(workspace.root_path, "__init__.py"))
134137
workspace.put_document(doc_uri, doc_str)
135-
workspace._config.update({"plugins": {"ruff": {"select": ["E", "F"]}}})
136138

137139
ruff_settings = get_ruff_cfg_settings(
138140
workspace, workspace.get_document(doc_uri), config_str
@@ -150,21 +152,52 @@ def test_ruff_config(workspace):
150152
mock_instance.communicate.return_value = [bytes(), bytes()]
151153

152154
doc = workspace.get_document(doc_uri)
153-
ruff_lint.pylsp_lint(workspace, doc)
155+
diags = ruff_lint.pylsp_lint(workspace, doc)
154156

155157
call_args = popen_mock.call_args[0][0]
156158
assert call_args == [
157159
"ruff",
158160
"--quiet",
159161
"--format=json",
160162
"--no-fix",
163+
"--force-exclude",
164+
"--stdin-filename",
165+
os.path.join(workspace.root_path, "__init__.py"),
161166
"--",
162167
"-",
163168
]
164169

165170
diags = ruff_lint.pylsp_lint(workspace, doc)
166171

172+
_list = []
173+
for diag in diags:
174+
_list.append(diag["code"])
175+
# Assert that ignore is working as intended
176+
assert "E402" in _list
177+
assert "F841" not in _list
178+
179+
# Excludes
180+
doc_uri = uris.from_fs_path(os.path.join(workspace.root_path, "blah/__init__.py"))
181+
workspace.put_document(doc_uri, doc_str)
182+
183+
ruff_settings = get_ruff_cfg_settings(
184+
workspace, workspace.get_document(doc_uri), config_str
185+
)
186+
187+
doc = workspace.get_document(doc_uri)
188+
diags = ruff_lint.pylsp_lint(workspace, doc)
189+
assert diags == []
190+
191+
# For per-file-ignores
192+
doc_uri_per_file_ignores = uris.from_fs_path(
193+
os.path.join(workspace.root_path, "blah/test_something.py")
194+
)
195+
workspace.put_document(doc_uri_per_file_ignores, doc_str)
196+
197+
doc = workspace.get_document(doc_uri)
198+
diags = ruff_lint.pylsp_lint(workspace, doc)
199+
167200
for diag in diags:
168-
assert diag["code"] != "F841"
201+
assert diag["code"] != "F401"
169202

170203
os.unlink(os.path.join(workspace.root_path, "pyproject.toml"))

0 commit comments

Comments
 (0)