Skip to content

Commit 35be74f

Browse files
authored
Avoid deleting unused imports on save within the editor (#245)
1 parent 8045b9d commit 35be74f

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

noxfile.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,27 @@ def test(s: Session) -> None:
2828
# For some sessions, set venv_backend="none" to simply execute scripts within the existing Poetry
2929
# environment. This requires that nox is run within `poetry shell` or using `poetry run nox ...`.
3030
@session(venv_backend="none")
31-
def fmt(s: Session) -> None:
32-
s.run("ruff", "check", ".", "--select", "I", "--fix")
33-
s.run("ruff", "format", ".")
31+
@parametrize(
32+
"command",
33+
[
34+
# During formatting, additionally sort imports and remove unused imports.
35+
[
36+
"ruff",
37+
"check",
38+
".",
39+
"--select",
40+
"I",
41+
"--select",
42+
"F401",
43+
"--extend-fixable",
44+
"F401",
45+
"--fix",
46+
],
47+
["ruff", "format", "."],
48+
],
49+
)
50+
def fmt(s: Session, command: list[str]) -> None:
51+
s.run(*command)
3452

3553

3654
@session(venv_backend="none")
@@ -47,7 +65,7 @@ def lint(s: Session, command: list[str]) -> None:
4765

4866
@session(venv_backend="none")
4967
def lint_fix(s: Session) -> None:
50-
s.run("ruff", "check", ".", "--fix")
68+
s.run("ruff", "check", ".", "--extend-fixable", "F401", "--fix")
5169

5270

5371
@session(venv_backend="none")

pyproject.toml

+9-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,15 @@ select = [
105105
"SIM", # flake8-simplify
106106
"TID", # flake8-tidy-imports
107107
]
108-
extend-ignore = ["RUF005", "RUF012"]
108+
extend-ignore = [
109+
"RUF005",
110+
"RUF012",
111+
]
112+
unfixable = [
113+
# Disable removing unused imports by default and only enable within nox so editors don't delete
114+
# unnused imports while the user is in the middle of editing a file on save.
115+
"F401",
116+
]
109117

110118
[tool.ruff.lint.isort]
111119
force-sort-within-sections = true

0 commit comments

Comments
 (0)