| name | ruff |
| description | Guide for using ruff, the extremely fast Python linter and formatter. Use this when linting, formatting, or fixing Python code. |
ruff
Ruff is an extremely fast Python linter and code formatter. It replaces Flake8,
isort, Black, pyupgrade, autoflake, and dozens of other tools.
When to use ruff
Always use ruff for Python linting and formatting, especially if you see:
[tool.ruff] section in pyproject.toml
- A
ruff.toml or .ruff.toml configuration file
However, avoid making unnecessary changes:
- Don't format unformatted code - If
ruff format --diff shows changes
throughout an entire file, the project likely isn't using ruff for formatting.
Skip formatting to avoid obscuring actual changes.
- Scope fixes to code being edited - Use
ruff check --diff to see fixes
relevant to the code you're changing. Only apply fixes to files you're
modifying unless the user explicitly asks for broader fixes.
How to invoke ruff
uv run ruff ... - Use when ruff is in the project's dependencies to ensure
you use the pinned version
uvx ruff ... - Use when ruff is not a project dependency, or for quick
one-off checks
ruff ... - Use if ruff is installed globally
Commands
Linting
ruff check .
ruff check path/to/file.py
ruff check --fix .
ruff check --fix --unsafe-fixes .
ruff check --watch .
ruff check --select E,F .
ruff check --ignore E501 .
ruff rule E501
ruff linter
Formatting
ruff format .
ruff format path/to/file.py
ruff format --check .
ruff format --diff .
Configuration
Ruff is configured in pyproject.toml or ruff.toml:
[tool.ruff.lint]
select = ["E", "F", "I", "UP"]
ignore = ["E501"]
[tool.ruff.lint.isort]
known-first-party = ["myproject"]
Migrating from other tools
Black → ruff format
black . → ruff format .
black --check . → ruff format --check .
black --diff . → ruff format --diff .
Flake8 → ruff check
flake8 . → ruff check .
flake8 --select E,F . → ruff check --select E,F .
flake8 --ignore E501 . → ruff check --ignore E501 .
isort → ruff check
isort . → ruff check --select I --fix .
isort --check . → ruff check --select I .
isort --diff . → ruff check --select I --diff .
Common patterns
Apply lint fixes before formatting
Run ruff check --fix before ruff format. Lint fixes can change code
structure (e.g., reordering imports), which formatting then cleans up.
ruff check --fix .
ruff format .
Applying and reviewing unsafe fixes
Ruff categorizes some auto-fixes as "unsafe" because they may change code
behavior, not just style. For example, removing unused imports could break code
that relies on side effects.
ruff check --fix --unsafe-fixes --diff .
ruff check --fix --unsafe-fixes .
Always review changes before applying --unsafe-fixes:
- Use
ruff rule <CODE> to understand why the fix is considered unsafe
- Verify the fix doesn't violate those assumptions in your code
Documentation
For detailed information, read the official documentation: