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 从公开仓库中收录这些内容。

举报或申请下架