Skip to content

Commit 0844fb1

Browse files
committed
Track applicability in fix, add unsafeFixes
1 parent 3b2c283 commit 0844fb1

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

pylsp_ruff/plugin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ def pylsp_code_actions(
214214
code_actions = []
215215
has_organize_imports = False
216216

217+
settings = load_settings(workspace=workspace, document_path=document.path)
218+
217219
for diagnostic in diagnostics:
218220
code_actions.append(
219221
create_disable_code_action(document=document, diagnostic=diagnostic)
@@ -222,6 +224,10 @@ def pylsp_code_actions(
222224
if diagnostic.data: # Has fix
223225
fix = converter.structure(diagnostic.data, RuffFix)
224226

227+
# Ignore fix if marked as unsafe and unsafe_fixes are disabled
228+
if fix.applicability != "safe" and not settings.unsafe_fixes:
229+
continue
230+
225231
if diagnostic.code == "I001":
226232
code_actions.append(
227233
create_organize_imports_code_action(
@@ -236,7 +242,6 @@ def pylsp_code_actions(
236242
),
237243
)
238244

239-
settings = load_settings(workspace=workspace, document_path=document.path)
240245
checks = run_ruff_check(document=document, settings=settings)
241246
checks_with_fixes = [c for c in checks if c.fix]
242247
checks_organize_imports = [c for c in checks_with_fixes if c.code == "I001"]

pylsp_ruff/ruff.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Edit:
1919
class Fix:
2020
edits: List[Edit]
2121
message: str
22+
applicability: str
2223

2324

2425
@dataclass

tests/test_code_actions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ def f():
115115
pass
116116
"""
117117
)
118+
expected_str_safe = dedent(
119+
"""
120+
def f():
121+
a = 2
122+
"""
123+
)
118124
workspace._config.update(
119125
{
120126
"plugins": {
@@ -129,6 +135,20 @@ def f():
129135
fixed_str = ruff_lint.run_ruff_fix(doc, settings)
130136
assert fixed_str == expected_str
131137

138+
workspace._config.update(
139+
{
140+
"plugins": {
141+
"ruff": {
142+
"unsafeFixes": False,
143+
}
144+
}
145+
}
146+
)
147+
_, doc = temp_document(codeaction_str, workspace)
148+
settings = ruff_lint.load_settings(workspace, doc.path)
149+
fixed_str = ruff_lint.run_ruff_fix(doc, settings)
150+
assert fixed_str == expected_str_safe
151+
132152

133153
def test_format_document_default_settings(workspace):
134154
_, doc = temp_document(import_str, workspace)

0 commit comments

Comments
 (0)