| name | python-ruff |
| description | This skill should be used when the user asks to "configure ruff", "set up ruff linting", "use ruff formatter", "replace flake8 with ruff", or needs guidance on Python code quality with Ruff linting and formatting best practices. |
Python Ruff — Linting and Formatting
Ruff is an extremely fast Python linter and code formatter written in Rust. It replaces Flake8, Black, isort, pydocstyle, pyupgrade, and autoflake with a single unified tool that runs 10–100x faster than any of them individually. Ruff supports over 800 built-in rules and provides automatic fix capabilities for many violations.
Installation
Install Ruff via pip, uv, or as a development dependency:
pip install ruff
uv add --dev ruff
Core Commands
ruff check
ruff check --fix
ruff check --unsafe-fixes
ruff check --fix --unsafe-fixes
ruff check --watch
ruff format
ruff format --check
ruff format --diff
Run both linting (with import sorting) and formatting in sequence:
ruff check --select I --fix
ruff format
Configuration
Ruff reads from pyproject.toml, ruff.toml, or .ruff.toml. All three support the same schema; ruff.toml and .ruff.toml omit the [tool.ruff] prefix.
Recommended Starter Configuration
[tool.ruff]
target-version = "py312"
line-length = 88
[tool.ruff.lint]
select = [
"E",
"F",
"UP",
"B",
"SIM",
"I",
]
ignore = ["E501"]
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
docstring-code-format = true
See references/configuration-guide.md for the full configuration reference.
Rule Selection Best Practices
Rule codes follow the pattern PREFIX + digits (e.g., F401 = Pyflakes unused import, E711 = pycodestyle comparison to None).
Start small, then expand:
select = ["E4", "E7", "E9", "F"]
select = ["E", "F", "UP", "B", "SIM", "I"]
select = ["ALL"]
ignore = ["D", "ANN", "COM812", "ISC001"]
Key rule prefixes:
| Prefix | Source | Purpose |
|---|
E/W | pycodestyle | Style errors and warnings |
F | Pyflakes | Logical errors, unused imports |
B | flake8-bugbear | Likely bugs and design issues |
UP | pyupgrade | Upgrade to modern Python syntax |
SIM | flake8-simplify | Code simplification |
I | isort | Import sorting |
N | pep8-naming | Naming convention checks |
D | pydocstyle | Docstring conventions |
ANN | flake8-annotations | Type annotation enforcement |
S | flake8-bandit | Security checks |
RUF | Ruff-native | Ruff-specific rules |
Use lint.select (not lint.extend-select) to make rule sets explicit. Avoid enabling ALL without carefully curating an ignore list, as it enables new rules on every Ruff upgrade.
See references/rule-categories.md for detailed rule guidance and common ignores.
Automatic Fixes
Ruff distinguishes between safe and unsafe fixes:
- Safe fixes preserve code behavior exactly — applied by default with
--fix
- Unsafe fixes may change runtime behavior (e.g., exception types, removed comments) — opt-in with
--unsafe-fixes
Promote or demote fix safety per rule:
[tool.ruff.lint]
extend-safe-fixes = ["UP"]
extend-unsafe-fixes = ["B"]
Disable auto-fix for specific rules while keeping them as violations:
[tool.ruff.lint]
fixable = ["ALL"]
unfixable = ["F401"]
Error Suppression
Line-level suppression
import os
x = 1
y = 1
Block-level suppression (preferred over blanket noqa)
LONG_CONSTANT_1 = "Lorem ipsum dolor sit amet, consectetur adipiscing..."
LONG_CONSTANT_2 = "Lorem ipsum dolor sit amet, consectetur adipiscing..."
File-level suppression
Per-file ignores in config (preferred)
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"tests/**/*.py" = ["S101", "ANN"]
"scripts/**/*.py" = ["T20"]
Detecting unused suppressions
ruff check --extend-select RUF100
ruff check --extend-select RUF100 --fix
Formatter Configuration
The Ruff formatter is a drop-in replacement for Black. Key options:
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
line-ending = "auto"
skip-magic-trailing-comma = false
docstring-code-format = true
Suppressing formatter
matrix = [1,0,0,
0,1,0,
0,0,1]
result = some_call()
Rules incompatible with the formatter
Disable these rules to avoid conflicts when using ruff format:
[tool.ruff.lint]
ignore = [
"W191",
"E111",
"COM812",
"ISC002",
"Q000",
"Q001",
"Q002",
]
CI and Pre-commit Integration
GitHub Actions
- name: Lint and format check
run: |
ruff check .
ruff format --check .
pre-commit hooks
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
Migrating an existing codebase
Auto-add noqa directives to all current violations, then clean up incrementally:
ruff check --add-noqa .
ruff check --extend-select RUF100 --fix .
Quick Reference
| Task | Command |
|---|
| Lint current directory | ruff check |
| Lint and fix safe violations | ruff check --fix |
| Format current directory | ruff format |
| Check formatting (CI mode) | ruff format --check |
| Sort imports only | ruff check --select I --fix |
| Explain a rule | ruff rule F401 |
| List all rules | ruff linter |
| Show active config for a file | ruff check --show-settings <file.py> |
| Flag unused noqa comments | ruff check --extend-select RUF100 |
Additional Resources
Reference Files
references/rule-categories.md — Detailed rule prefixes, common ignores, and per-category guidance
references/configuration-guide.md — Full configuration options for linter and formatter
Example Files
examples/pyproject.toml — Production-ready pyproject.toml configuration
examples/ruff.toml — Standalone ruff.toml configuration (monorepo root)
examples/pre-commit-config.yaml — pre-commit hooks configuration