Skip to content

Commit

Permalink
Avoid deleting unused imports on save within an editor
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthagen committed Jan 9, 2025
1 parent 8045b9d commit 0409f80
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
26 changes: 22 additions & 4 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,27 @@ def test(s: Session) -> None:
# For some sessions, set venv_backend="none" to simply execute scripts within the existing Poetry
# environment. This requires that nox is run within `poetry shell` or using `poetry run nox ...`.
@session(venv_backend="none")
def fmt(s: Session) -> None:
s.run("ruff", "check", ".", "--select", "I", "--fix")
s.run("ruff", "format", ".")
@parametrize(
"command",
[
# During formatting, additionally sort imports and remove unused imports.
[
"ruff",
"check",
".",
"--select",
"I",
"--select",
"F401",
"--extend-fixable",
"F401",
"--fix",
],
["ruff", "format", "."],
],
)
def fmt(s: Session, command: list[str]) -> None:
s.run(*command)


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

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


@session(venv_backend="none")
Expand Down
10 changes: 9 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ select = [
"SIM", # flake8-simplify
"TID", # flake8-tidy-imports
]
extend-ignore = ["RUF005", "RUF012"]
extend-ignore = [
"RUF005",
"RUF012",
]
unfixable = [
# Disable unused import fixing by default and only enable in `nox -s fmt` so editors don't
# delete unnused imports while in the middle of editing a file on save.
"F401",
]

[tool.ruff.lint.isort]
force-sort-within-sections = true
Expand Down

0 comments on commit 0409f80

Please sign in to comment.