Ruff

作者 astral-shf3ce88a7ba83無授權條款313 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫7 個月前更新

Guide for using ruff, the extremely fast Python linter and formatter. Use this when linting, formatting, or fixing Python code.

AI 產生的概覽

介紹如何使用 ruff 這款快速的 Python 程式碼檢查與格式化工具來檢查、格式化和修正 Python 程式碼。

功能
說明如何呼叫 ruff 進行 Python 程式碼檢查與格式化,包括檢查、自動修正、監看和格式化檔案的指令。涵蓋在 pyproject.toml 或 ruff.toml 中的設定、從 Black、Flake8 和 isort 移轉的對應關係,以及先執行檢查修正再格式化的常見做法。也建議將修正範圍限制在正在編輯的程式碼,並在套用不安全修正前先審閱。
適用情境
在檢查、格式化或修正 Python 程式碼時使用,尤其是專案已設定 ruff 的情況。也適用於從 Black、Flake8 或 isort 移轉,或需要決定如何在專案中執行 ruff 時。
執行需求
需要可用的 ruff,可作為專案相依性透過 uv 執行、透過 uvx 執行,或全域安裝。不附帶指令碼,僅為說明文件。

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

bash
ruff check .                  # Check all files in current directoryruff check path/to/file.py    # Check specific fileruff check --fix .            # Auto-fix fixable violationsruff check --fix --unsafe-fixes .  # Include unsafe fixes (review changes!)ruff check --watch .          # Watch for changes and re-lintruff check --select E,F .     # Only check specific rulesruff check --ignore E501 .    # Ignore specific rulesruff rule E501                # Explain a specific ruleruff linter                   # List available linters

Formatting

bash
ruff format .                 # Format all filesruff format path/to/file.py   # Format specific fileruff format --check .         # Check if files are formatted (no changes)ruff format --diff .          # Show formatting diff without applying

Configuration

Ruff is configured in pyproject.toml or ruff.toml:

toml
# pyproject.toml[tool.ruff.lint]select = ["E", "F", "I", "UP"]  # Enable specific rule setsignore = ["E501"]               # Ignore specific rules
[tool.ruff.lint.isort]known-first-party = ["myproject"]

Migrating from other tools

Black → ruff format

bash
black .                       → ruff format .black --check .               → ruff format --check .black --diff .                → ruff format --diff .

Flake8 → ruff check

bash
flake8 .                      → ruff check .flake8 --select E,F .         → ruff check --select E,F .flake8 --ignore E501 .        → ruff check --ignore E501 .

isort → ruff check

bash
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.

bash
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.

bash
ruff check --fix --unsafe-fixes --diff .  # Preview changes firstruff check --fix --unsafe-fixes .         # Apply changes

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:

來源與署名

來源:astral-sh/claude-code-plugins位於plugins/astral/skills/ruff提交f3ce88a

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架