Skip to content

Commit c8dae9d

Browse files
committed
Fix #2.
Sets config options (except `executable`) to `None` if `pyproject.toml` file is detected.
1 parent 421aa49 commit c8dae9d

File tree

2 files changed

+42
-94
lines changed

2 files changed

+42
-94
lines changed

pylsp_ruff/ruff_lint.py

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
import json
22
import logging
3-
import sys
43
from pathlib import PurePath
54
from subprocess import PIPE, Popen, SubprocessError
65

76
from pylsp import hookimpl, lsp
87
from pylsp._utils import find_parents
98
from pylsp.workspace import Document, Workspace
109

11-
# Use built-in tomllib for python>=3.11
12-
if sys.version_info >= (3, 11):
13-
try:
14-
import tomllib
15-
except ImportError:
16-
import tomli as tomllib
17-
else:
18-
import tomli as tomllib
19-
2010
log = logging.getLogger(__name__)
2111

2212
UNNECESSITY_CODES = {
@@ -232,36 +222,38 @@ def load_config(workspace: Workspace, document: Document) -> dict:
232222
config = workspace._config
233223
_settings = config.plugin_settings("ruff", document_path=document.path)
234224

235-
# Default values are given by ruff
236-
settings = {
237-
"config": _settings.get("config", None),
238-
"exclude": _settings.get("exclude", None),
239-
"executable": _settings.get("executable", "ruff"),
240-
"ignore": _settings.get("ignore", None),
241-
"line-length": _settings.get("lineLength", None),
242-
"per-file-ignores": _settings.get("perFileIgnores", None),
243-
"select": _settings.get("select", None),
244-
}
245-
246225
pyproject_file = find_parents(
247226
workspace.root_path, document.path, ["pyproject.toml"]
248227
)
249228

250-
# Load config from pyproject file if it exists
229+
# Check if pyproject is present, ignore user settings if toml exists
251230
if pyproject_file:
252-
try:
253-
log.debug(f"Found pyproject file: {str(pyproject_file[0])}")
254-
255-
with open(str(pyproject_file[0]), "rb") as pyproject_toml:
256-
toml_dict = tomllib.load(pyproject_toml)
257-
258-
toml_config = toml_dict.get("tool", {}).get("ruff", {})
259-
260-
# Update settings with local project settings
261-
for key, value in toml_config.items():
262-
settings[key] = value
231+
log.debug(
232+
f"Found pyproject file: {str(pyproject_file[0])}, "
233+
+ "skipping pylsp config."
234+
)
235+
236+
# Leave config to pyproject.toml
237+
settings = {
238+
"config": None,
239+
"exclude": None,
240+
"executable": _settings.get("executable", "ruff"),
241+
"ignore": None,
242+
"line-length": None,
243+
"per-file-ignores": None,
244+
"select": None,
245+
}
263246

264-
except (tomllib.TOMLDecodeError, OSError) as e:
265-
log.warning(f"Failed loading {str(pyproject_file)}: {e}")
247+
else:
248+
# Default values are given by ruff
249+
settings = {
250+
"config": _settings.get("config", None),
251+
"exclude": _settings.get("exclude", None),
252+
"executable": _settings.get("executable", "ruff"),
253+
"ignore": _settings.get("ignore", None),
254+
"line-length": _settings.get("lineLength", None),
255+
"per-file-ignores": _settings.get("perFileIgnores", None),
256+
"select": _settings.get("select", None),
257+
}
266258

267259
return settings

tests/test_ruff_lint.py

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,34 @@ def get_ruff_cfg_settings(workspace, doc, config_str):
116116
return ruff_lint.load_config(workspace, doc)
117117

118118

119-
def test_ruff_multiline(workspace):
119+
def test_ruff_config(workspace):
120120
config_str = r"""[tool.ruff]
121+
ignore = ["F481"]
121122
exclude = [
122123
"blah",
123124
"file_2.py"
124125
]
126+
[tool.ruff.per-file-ignores]
127+
"__init__.py" = ["F401", "E402"]
128+
"test_something.py" = ["E402"]
125129
"""
126130

127131
doc_str = "print('hi')\nimport os\n"
128132

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

132137
ruff_settings = get_ruff_cfg_settings(
133138
workspace, workspace.get_document(doc_uri), config_str
134139
)
135140

136-
assert "exclude" in ruff_settings
137-
assert len(ruff_settings["exclude"]) == 2
141+
# Check that user config is ignored
142+
for key, value in ruff_settings.items():
143+
if key == "executable":
144+
assert value == "ruff"
145+
continue
146+
assert value is None
138147

139148
with patch("pylsp_ruff.ruff_lint.Popen") as popen_mock:
140149
mock_instance = popen_mock.return_value
@@ -149,66 +158,13 @@ def test_ruff_multiline(workspace):
149158
"--quiet",
150159
"--format=json",
151160
"--no-fix",
152-
"--exclude=blah,file_2.py",
153161
"--",
154162
"-",
155163
]
156164

157-
os.unlink(os.path.join(workspace.root_path, "pyproject.toml"))
158-
159-
160-
def test_ruff_per_file_ignores(workspace):
161-
config_str = r"""[tool.ruff]
162-
ignore = ["F403"]
163-
exclude = [
164-
"file_1.py",
165-
"file_2.py",
166-
]
167-
[tool.ruff.per-file-ignores]
168-
"__init__.py" = ["F401", "E402"]
169-
"test_something.py" = ["E402"]
170-
"""
171-
172-
doc_str = "print('hi')\nimport os\n"
173-
174-
doc_uri = uris.from_fs_path(os.path.join(workspace.root_path, "blah/__init__.py"))
175-
workspace.put_document(doc_uri, doc_str)
176-
177-
ruff_settings = get_ruff_cfg_settings(
178-
workspace, workspace.get_document(doc_uri), config_str
179-
)
180-
181-
assert "per-file-ignores" in ruff_settings
182-
assert len(ruff_settings["per-file-ignores"]) == 2
183-
assert "exclude" in ruff_settings
184-
assert len(ruff_settings["exclude"]) == 2
185-
186-
doc = workspace.get_document(doc_uri)
187-
res = ruff_lint.pylsp_lint(workspace, doc)
188-
assert not res
189-
190-
os.unlink(os.path.join(workspace.root_path, "pyproject.toml"))
191-
192-
193-
def test_per_file_ignores_alternative_syntax(workspace):
194-
config_str = r"""[tool.ruff.per-file-ignores]
195-
"__init__.py" = ["F401", "E402"]
196-
"""
197-
198-
doc_str = "print('hi')\nimport os\n"
199-
200-
doc_uri = uris.from_fs_path(os.path.join(workspace.root_path, "blah/__init__.py"))
201-
workspace.put_document(doc_uri, doc_str)
202-
203-
ruff_settings = get_ruff_cfg_settings(
204-
workspace, workspace.get_document(doc_uri), config_str
205-
)
206-
207-
assert "per-file-ignores" in ruff_settings
208-
assert ruff_settings["per-file-ignores"] == {"__init__.py": ["F401", "E402"]}
165+
diags = ruff_lint.pylsp_lint(workspace, doc)
209166

210-
doc = workspace.get_document(doc_uri)
211-
res = ruff_lint.pylsp_lint(workspace, doc)
212-
assert not res
167+
for diag in diags:
168+
assert diag["code"] != "F841"
213169

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

0 commit comments

Comments
 (0)