Ruff Python lint and format
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
ruff checkruff check --fix
ruff formatruff format --check
ruff check --select I --fix
ruff rule <code>
ruff check --add-noqa
ruff check --watch
Concepts
- Config pyproject.toml / ruff.toml
- ruff check violations
- ruff format layout
- Clean tree commit
Check
ruff checkruff check <path>...
ruff check --fix
ruff check --diff
ruff check --unsafe-fixes --fix
ruff check --show-fixes
ruff check --fix-only
ruff check --watch
ruff check -ruff check --stdin-filename <name> -
ruff check --exit-zero
ruff check --exit-non-zero-on-fix
ruff check --preview
Format
ruff formatruff format <path>...
ruff format --check
ruff format --diff
ruff format --line-length <n>
ruff format -ruff format --range <l1>-<l2>
<file>
ruff format --exit-non-zero-on-format
| Comment | Effect |
|---|---|
# fmt: off / # fmt: on |
skip a region |
# fmt: skip |
skip one statement |
Rule selection
--select <codes>
--extend-select <codes>
--ignore <codes>
--select ALL--per-file-ignores <glob>:<code>
--extend-per-file-ignores <glob>:<code>
--fixable <codes> / --unfixable <codes>
--target-version <py3xx>
--statisticsruff check --select I --fix
ruff check --select UP --fix
| Prefix | From |
|---|---|
E / W |
pycodestyle |
F |
Pyflakes |
I |
isort |
B |
bugbear |
UP |
pyupgrade |
SIM |
simplify |
N |
pep8-naming |
C4 |
comprehensions |
Suppressing
# noqa: <code>
# noqa# ruff: noqa: <code>
ruff check --add-noqa
ruff check --add-noqa="<reason>"
ruff check --ignore-noqa
Explore
ruff rule <code>
ruff rule --allruff linterruff configruff config <option>
ruff check --show-settings <file>
ruff check --show-files
ruff cleanruff versionOutput and files
--output-format <fmt>
-o <file> / --output-file <file>
--exclude <pattern>
--extend-exclude <pattern>
--force-exclude--no-respect-gitignore
--no-cache--config <file|key=value>
--isolatedConfig
[tool.ruff]line-length = 100target-version = "py312"extend-exclude = ["migrations"][tool.ruff.lint]select = ["E4", "E7", "E9", "F", "I", "B"]ignore = ["B008"]
[tool.ruff.lint.per-file-ignores]"tests/**" = ["S101"]"__init__.py" = ["F401"]
[tool.ruff.format]quote-style = "double"indent-style = "space"docstring-code-format = true
[tool.ruff.lint.isort]known-first-party = ["myapp"]
line-length = 100[lint]extend-select = ["I"][format]quote-style = "single"
Editors and CI
ruff serverrepos:- repo: https://github.com/astral-sh/ruff-pre-commit rev: <tag> hooks: - id: ruff-check args: [--fix] - id: ruff-format
- run: pip install ruff- run: ruff check --output-format=github- run: ruff format --check
Recipes
ruff check --statisticsruff check --add-noqaruff format# then tighten rules gradually
ruff check --fixruff formatgit diff --stat
ruff check --extend-select B,SIM --statisticsruff check --extend-select B --diff
ruff check --output-format=conciseruff format --check --diff



