Skip to content

Commit eff74e1

Browse files
authored
Update 'unsafe' code actions (#55) (#62)
- Update unsafe code actions to contain `(unsafe)` at the end of the message - Prevent the `Fix All` code action from applying unsafe codeactions - Update the README to mention this behaviour
1 parent 9ba6e51 commit eff74e1

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ lspconfig.pylsp.setup {
5050
}
5151
```
5252

53+
## Code actions
54+
55+
`python-lsp-ruff` supports code actions as given by possible fixes by `ruff`. `python-lsp-ruff` also supports [unsafe fixes](https://docs.astral.sh/ruff/linter/#fix-safety).
56+
Fixes considered unsafe by `ruff` are marked `(unsafe)` in the code action.
57+
The `Fix all` code action *only* consideres safe fixes.
58+
5359
## Configuration
5460

5561
Configuration options can be passed to the python-language-server. If a `pyproject.toml`

pylsp_ruff/plugin.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,10 @@ def pylsp_code_actions(
238238
if diagnostic.data: # Has fix
239239
fix = converter.structure(diagnostic.data, RuffFix)
240240

241-
# Ignore fix if marked as unsafe and unsafe_fixes are disabled
242-
if fix.applicability != "safe" and not settings.unsafe_fixes:
243-
continue
241+
if fix.applicability == "unsafe":
242+
if not settings.unsafe_fixes:
243+
continue
244+
fix.message += " (unsafe)"
244245

245246
if diagnostic.code == "I001":
246247
code_actions.append(
@@ -359,6 +360,9 @@ def create_fix_all_code_action(
359360
title = "Ruff: Fix All"
360361
kind = CodeActionKind.SourceFixAll
361362

363+
# No unsafe fixes for 'Fix all', see https://github.com/python-lsp/python-lsp-ruff/issues/55
364+
settings.unsafe_fixes = False
365+
362366
new_text = run_ruff_fix(document=document, settings=settings)
363367
range = Range(
364368
start=Position(line=0, character=0),

tests/test_code_actions.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def f():
4545
codeactions = [
4646
"Ruff (F401): Remove unused import: `os`",
4747
"Ruff (F401): Disable for this line",
48-
"Ruff (F841): Remove assignment to unused variable `a`",
48+
"Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
4949
"Ruff (F841): Disable for this line",
5050
"Ruff: Fix All",
5151
]
@@ -70,7 +70,9 @@ def temp_document(doc_text, workspace):
7070
def test_ruff_code_actions(workspace):
7171
_, doc = temp_document(codeaction_str, workspace)
7272

73-
workspace._config.update({"plugins": {"ruff": {"select": ["F"]}}})
73+
workspace._config.update(
74+
{"plugins": {"ruff": {"select": ["F"], "unsafeFixes": True}}}
75+
)
7476
diags = ruff_lint.pylsp_lint(workspace, doc)
7577
range_ = cattrs.unstructure(
7678
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
@@ -79,8 +81,8 @@ def test_ruff_code_actions(workspace):
7981
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
8082
)
8183
actions = converter.structure(actions, List[CodeAction])
82-
for action in actions:
83-
assert action.title in codeactions
84+
action_titles = list(map(lambda action: action.title, actions))
85+
assert sorted(codeactions) == sorted(action_titles)
8486

8587

8688
def test_import_action(workspace):
@@ -104,8 +106,8 @@ def test_import_action(workspace):
104106
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
105107
)
106108
actions = converter.structure(actions, List[CodeAction])
107-
for action in actions:
108-
assert action.title in codeactions_import
109+
action_titles = list(map(lambda action: action.title, actions))
110+
assert sorted(codeactions_import) == sorted(action_titles)
109111

110112

111113
def test_fix_all(workspace):

0 commit comments

Comments
 (0)