| name | ruff-formatter |
| description | Fix Python code formatting issues using the Ruff formatter. Use when: (1) Formatting errors are detected by ruff format --check, (2) Python files need to be formatted to match project style, (3) Pre-commit hooks or CI fail due to formatting issues. |
Ruff Formatter
Fast Python code formatter, drop-in replacement for Black with >99.9% compatibility.
Quick Reference
ruff format .
ruff format path/to/file.py
ruff format --check .
ruff format --diff .
Fixing Formatting Issues
When ruff format --check fails:
- Run
ruff format . to auto-fix all formatting
- Review changes with
git diff
- Commit the formatted code
For import sorting issues, run linter first:
ruff check --select I --fix .
ruff format .
Format Suppression
Disable formatting for specific code:
matrix = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
]
x = 1
Configuration
In pyproject.toml or ruff.toml:
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
line-length = 88
docstring-code-format = true
Exit Codes
- 0: Success (files formatted or already formatted)
- 1: With
--check: files need formatting
- 2: Error (invalid config, CLI error)