| name | ruff |
| description | This skill should be used when users need to lint, format, or validate Python code using the Ruff command-line tool. Use this skill for tasks involving Python code quality checks, automatic code formatting, enforcing style rules (PEP 8), identifying bugs and security issues, or modernizing Python code. This skill should be invoked PROACTIVELY whenever Python code is written or modified to ensure code quality. |
Ruff Skill
This skill provides expertise in using Ruff, an extremely fast Python linter and code formatter written in Rust. Ruff combines the functionality of multiple tools (Flake8, isort, Black, and more) into a single, high-performance package.
IMPORTANT: Proactive Usage
You MUST use this skill proactively in the following scenarios:
- After writing or modifying Python code - Always run ruff format and ruff check --fix after creating or editing Python files
- Before committing code - Ensure all Python code passes linting and formatting checks
- When encountering linting errors - Immediately fix them using this skill's guidance
- During code reviews - Check code quality before finalizing changes
Default workflow when writing Python code:
uv run ruff format .
uv run ruff check --fix .
uv run ruff check .
Use the project's just commands when available:
just fmt-python
just lint-python
About Ruff
Ruff is designed to replace multiple Python development tools with a single, fast, and comprehensive solution. It's 10-100x faster than traditional Python linters while providing comparable or better functionality.
Key Capabilities
- Linting: Check Python code against 800+ lint rules from popular tools
- Formatting: Format Python code with Black-compatible output
- Auto-fixing: Automatically fix many linting issues
- Import Sorting: Organize imports (isort-compatible)
- Configuration: Highly customizable via
pyproject.toml or ruff.toml
- Fast: Written in Rust for exceptional performance
- All-in-one: Replaces Flake8, Black, isort, pydocstyle, pyupgrade, and more
When to Use This Skill
Use this skill when users:
- Need to lint or format Python code
- Want to enforce Python code quality standards (PEP 8, etc.)
- Need to identify bugs, security issues, or code smells
- Want to automatically fix common Python issues
- Need to format and organize imports
- Want to modernize Python code (e.g., upgrade syntax)
- Need to integrate linting/formatting into CI/CD pipelines
- Ask about Python code quality best practices
- Want to configure custom linting or formatting rules
- Need faster alternatives to Flake8, Black, or isort
How to Use This Skill
Basic Ruff Workflow
Ruff has two main commands:
ruff check: Lint Python code (finds issues)
ruff format: Format Python code (fixes style)
Linting with ruff check
Basic Linting
Check a single file:
ruff check script.py
Check a directory:
ruff check src/
ruff check .
Check multiple paths:
ruff check src/ tests/ script.py
Auto-fixing Issues
Fix issues automatically where possible:
ruff check --fix script.py
ruff check --fix src/
The --fix option will modify files in place to resolve fixable issues.
Unsafe Fixes
Some fixes are considered "unsafe" because they may change code behavior. To apply these:
ruff check --fix --unsafe-fixes script.py
Always review unsafe fixes carefully before committing.
Show Fixes Without Applying
Preview what fixes would be applied:
ruff check --diff script.py
Statistics and Reporting
Show statistics about issues found:
ruff check --statistics src/
Output in different formats:
ruff check --output-format=json src/
ruff check --output-format=github src/
ruff check --output-format=gitlab src/
ruff check --output-format=junit src/
Rule Selection
Select specific rules or categories:
ruff check --select F401 src/
ruff check --select E,F,W src/
ruff check --ignore E501 src/
ruff check --extend-select B src/
Common rule prefixes:
- F: Pyflakes (errors, undefined names, unused imports)
- E/W: pycodestyle errors and warnings (PEP 8)
- C90: mccabe (complexity)
- I: isort (import sorting)
- N: pep8-naming
- UP: pyupgrade (modernize syntax)
- B: flake8-bugbear (likely bugs)
- S: flake8-bandit (security)
- A: flake8-builtins
- Q: flake8-quotes
- SIM: flake8-simplify
Watch Mode
Continuously watch for changes and re-lint:
ruff check --watch src/
Formatting with ruff format
Basic Formatting
Format a single file:
ruff format script.py
Format a directory:
ruff format src/
ruff format .
Check Format Without Modifying
Check if files need formatting:
ruff format --check src/
This is useful in CI/CD to verify code is formatted without modifying files.
Show Formatting Differences
Show what changes would be made:
ruff format --diff src/
Format stdin
Format code from standard input:
echo "x=1" | ruff format -
cat script.py | ruff format -
Configuration
Configuration Files
Ruff looks for configuration in the following order:
ruff.toml
.ruff.toml
pyproject.toml (under [tool.ruff])
Basic Configuration (pyproject.toml)
[tool.ruff]
line-length = 100
target-version = "py311"
select = [
"E",
"W",
"F",
"I",
"B",
"UP",
"S",
"SIM",
]
ignore = [
"E501",
]
exclude = [
".git",
".venv",
"__pycache__",
"build",
"dist",
]
fixable = ["ALL"]
unfixable = []
[tool.ruff.per-file-ignores]
"tests/**/*.py" = ["S101"]
"__init__.py" = ["F401"]
[tool.ruff.format]
quote-style = "single"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
[tool.ruff.isort]
known-first-party = ["myapp"]
Standalone ruff.toml
line-length = 100
target-version = "py311"
select = ["E", "F", "I", "B", "UP"]
ignore = ["E501"]
[format]
quote-style = "double"
indent-style = "space"
Command-Line Configuration
Override configuration from command line:
ruff check --line-length=120 src/
ruff check --select=E,F,I src/
ruff check --target-version=py39 src/
Common Workflows
Full Code Quality Check
Run both linting and formatting checks:
ruff format --check .
ruff check .
Auto-fix Everything
Fix and format all code:
ruff check --fix .
ruff format .
Pre-commit Integration
Add to .pre-commit-config.yaml:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
CI/CD Integration
GitHub Actions example:
- name: Ruff Linter
run: ruff check --output-format=github .
- name: Ruff Formatter
run: ruff format --check .
Migration from Other Tools
Replace existing tools with Ruff:
From Black + isort + Flake8:
black .
isort .
flake8 .
ruff format .
ruff check --select I --fix .
ruff check .
Configuration Migration:
[tool.ruff]
line-length = 88
[tool.ruff.format]
quote-style = "double"
[tool.ruff.isort]
known-first-party = ["myapp"]
force-single-line = false
[tool.ruff]
select = ["E", "F", "W", "C90"]
ignore = ["E203", "E501", "W503"]
Gradual Adoption
Introduce Ruff gradually to existing projects:
ruff check --select=E,F .
ruff check --select=E,F,B .
ruff check --select=E,F,B,UP .
ruff format .
Use --add-noqa to add # noqa comments for existing violations:
ruff check --add-noqa src/
This allows you to enforce rules for new code while temporarily allowing existing violations.
Fix Specific Issue Types
Fix only certain categories of issues:
ruff check --select I --fix .
ruff check --select F401,F841 --fix .
ruff check --select UP --fix .
Rule Categories
Essential Rules (Start Here)
[tool.ruff]
select = [
"F",
"E",
"I",
]
Recommended Rules (Add Next)
[tool.ruff]
select = [
"F", "E", "I",
"W",
"UP",
"B",
"SIM",
]
Advanced Rules (Optional)
[tool.ruff]
select = [
"F", "E", "I", "W", "UP", "B", "SIM",
"S",
"N",
"C90",
"A",
"Q",
"RET",
"ARG",
"PTH",
"PD",
]
Inline Configuration
Disable Rules for Lines
Use # noqa comments to disable rules:
import os
import os
x = 1
Per-file Ignores in Config
[tool.ruff.per-file-ignores]
"tests/*.py" = ["S101", "PLR2004"]
"__init__.py" = ["F401"]
"scripts/*.py" = ["T201"]
Troubleshooting
Too Many Issues
If you're overwhelmed by issues on an existing project:
-
Start with critical issues only:
ruff check --select=F,E .
-
Add noqa comments to existing violations:
ruff check --add-noqa .
-
Fix auto-fixable issues first:
ruff check --fix .
-
Enable rules gradually over time
Configuration Not Loading
If configuration isn't being applied:
- Check file name:
ruff.toml, .ruff.toml, or pyproject.toml
- Validate TOML syntax:
ruff check --config=ruff.toml .
- Use absolute paths in
exclude patterns if relative paths don't work
- Check for conflicting configurations in parent directories
Formatter vs Black Differences
Ruff formatter aims for 99% compatibility with Black. Known differences:
- Magic trailing comma handling in some edge cases
- Line break decisions in complex nested structures
To report differences: use --diff and compare with Black output
False Positives
If Ruff reports false positives:
- Use
# noqa comments for specific exceptions
- Configure per-file ignores for patterns
- Report issues to the Ruff project
Performance Issues
Ruff is typically very fast, but if experiencing slowness:
- Exclude large directories (venv, node_modules, build)
- Use
.ruffignore file for complex exclusion patterns
- Check for large files or deeply nested directories
Best Practices
- Start Simple: Begin with basic rules (F, E, I) and expand gradually
- Format First: Run
ruff format before ruff check to avoid style conflicts
- Use --fix Liberally: Most auto-fixes are safe and save time
- Review Unsafe Fixes: Always review
--unsafe-fixes changes before committing
- Configure Line Length: Set
line-length to match your team's preference
- Enable in CI/CD: Enforce checks in CI to maintain code quality
- Per-file Ignores: Use per-file configuration for test files, scripts, etc.
- Commit Configuration: Keep
ruff.toml or pyproject.toml in version control
- Document Exceptions: Comment why specific rules are disabled
- Keep Updated: Ruff evolves quickly; update regularly for new rules and fixes
Integration with IDEs
VS Code
Install the official Ruff extension:
{
"ruff.enable": true,
"ruff.organizeImports": true,
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
},
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
PyCharm/IntelliJ
Configure Ruff as an external tool or use the Ruff plugin.
Vim/Neovim
Use ALE, null-ls, or native LSP integration with Ruff's language server.
Installation
Ruff can be installed via multiple methods:
Using pip:
pip install ruff
Using pipx (recommended for CLI tool):
pipx install ruff
Using uv:
uv tool install ruff
Using Homebrew:
brew install ruff
Using Conda:
conda install -c conda-forge ruff
Verify installation:
ruff --version
ruff check --help
ruff format --help
Resources