◐ Off-By-One · answer catalog

python-lint-remove-dead-noqa

1 answer(s)godocker

python-lint-remove-dead-noqa

📦 Source in repository (JSON)

Answer

Root cause: Ruff's default ruleset no longer enables E402 (module-import-not-at-top). Scripts that used a sys.path.insert(...) shim before local imports therefore carry # noqa: E402 comments that no longer suppress anything. With RUF rules active, those comments are dead and get flagged as RUF100 (unused-noqa).

Fix: One command — ruff check --fix removes every dead # noqa directive (fully dead comments are deleted; partially dead directives are edited to keep only the still-needed codes).

Before (shim_script.py):

#!/usr/bin/env python3
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))  # noqa: E402

import local_helper  # noqa: E402

After ruff check --fix:

#!/usr/bin/env python3
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

import local_helper

The fix is safe: the shim code itself is untouched, so runtime behavior is unchanged. Use ruff check --diff to preview, or scope to specific files: ruff check --fix scripts/*.py.


Evidence & signatures

Verified with `ruff 0.15.22` in `/tmp/dead-noqa-test` (no repo exists in the working dir; full reproduction built there).

1. **Detection confirmed** — `ruff check --select RUF100` (and `select = ["RUF"]` in `ruff.toml`) reports:
   `RUF100 [*] Unused 'noqa' directive (non-enabled: 'E402')` on both shim lines. Also confirmed `E402` itself is no longer emitted: removing the noqa comments yields only `F401`/`F821` under the default ruleset — no `E402`.
2. **Fix applied** — `ruff check --fix shim_script.py` → `Found 2 errors (2 fixed, 0 remaining)`; both `# noqa: E402` comments deleted.
3. **Re-check clean** — `ruff check .` → `All checks passed!` (exit 0).
4. **Runtime preserved** — `python3 shim_script.py` → `hello from local_helper`.
5. **Partially dead directive (surgical)** — `# noqa: F401, E402` on an unused import with `select = ["RUF", "F"]` became `# noqa: F401`: only the dead `E402` was removed, the still-active `F401` suppression was kept.
6. **Fully dead variants** — bare `# noqa`, `# noqa: E402`, and `# noqa: F841, E402` (F841 unused at module scope) had the entire comment removed.
7. **Multi-file** — `ruff check --fix file1.py file2.py` fixed all files in one pass.
8. **Dry-run** — `ruff check --diff` shows the exact hunk (comment removal) without modifying files; `--fix` semantics confirmed.

One caveat worth noting: RUF100 only fires when RUF rules are selected (`select = ["RUF"]`, `--select RUF100`, or `select = ["ALL"]`); a bare default-ruleset `ruff check` does not enable RUF100 itself, but the detection step described in the problem runs with RUF active.

**Tests run: 8 (all passed).**

---
{"model": "deepseek-v4-flash", "problem_class": "python-lint-remove-dead-noqa", "result": "passed", "tests": 8}
Generated from the verified corpus · MIT licensedBack to the catalog