astral-sh / ruff
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Ruff Docs | Playground An extremely fast Python linter and code formatter, written in Rust. Linting the CPython codebase from scratch. ⚡️ 10-100x faster than existing linters (like Flake8) and formatters (like Black) 🐍 Installable via pip 🛠️ pyproject.toml support 🤝 Python 3.15 compatibility ⚖️ Drop-in parity with Flake8, isort, and Black 📦 Built-in caching, to avoid re-analyzing unchanged files 🔧 Fix support, for automatic error correction (e.g., automatically remove unused imports) 📏 Over 900 built-in rules, with native re-implementations of popular Flake8 plugins, like flake8-bugbear ⌨️ First-party editor integrations for VS Code and more 🌎 Monorepo-friendly, with hierarchical and cascading configuration
Ruff 文档 | Playground:一个用 Rust 编写的极速 Python 代码检查器(linter)和格式化工具。它能从零开始检查 CPython 代码库。⚡️ 比现有的检查器(如 Flake8)和格式化工具(如 Black)快 10 到 100 倍。🐍 可通过 pip 安装。🛠️ 支持 pyproject.toml。🤝 兼容 Python 3.15。⚖️ 与 Flake8、isort 和 Black 功能对等。📦 内置缓存,避免重复分析未更改的文件。🔧 支持自动修复错误(例如自动删除未使用的导入)。📏 拥有超过 900 条内置规则,并原生重新实现了流行的 Flake8 插件(如 flake8-bugbear)。⌨️ 提供 VS Code 等编辑器的官方集成。🌎 对 Monorepo 友好,支持分层和级联配置。
Ruff aims to be orders of magnitude faster than alternative tools while integrating more functionality behind a single, common interface. Ruff can be used to replace Flake8 (plus dozens of plugins), Black, isort, pydocstyle, pyupgrade, autoflake, and more, all while executing tens or hundreds of times faster than any individual tool. Ruff is actively developed and used in major open-source projects like: Apache Airflow Apache Superset FastAPI Hugging Face Pandas SciPy Ruff is backed by Astral, the creators of uv and ty. Read the launch post, or the original project announcement.
Ruff 的目标是在提供比替代工具快几个数量级的速度的同时,通过单一的通用接口集成更多功能。Ruff 可以用来替代 Flake8(及其数十个插件)、Black、isort、pydocstyle、pyupgrade、autoflake 等工具,且执行速度比任何单一工具快几十倍甚至几百倍。Ruff 正在积极开发中,并被 Apache Airflow、Apache Superset、FastAPI、Hugging Face、Pandas 和 SciPy 等大型开源项目所使用。Ruff 由 uv 和 ty 的创建者 Astral 提供支持。请阅读发布博文或原始项目公告。
Testimonials Sebastián Ramírez, creator of FastAPI: Ruff is so fast that sometimes I add an intentional bug in the code just to confirm it’s actually running and checking the code. Nick Schrock, founder of Elementl, co-creator of GraphQL: Why is Ruff a gamechanger? Primarily because it is nearly 1000x faster. Literally. Not a typo. On our largest module (dagster itself, 250k LOC) pylint takes about 2.5 minutes, parallelized across 4 cores on my M1. Running ruff against our entire codebase takes .4 seconds.
用户评价:FastAPI 的创建者 Sebastián Ramírez 表示:“Ruff 太快了,以至于我有时会故意在代码中加入一个 bug,只是为了确认它确实在运行并检查代码。” Elementl 的创始人、GraphQL 的共同创建者 Nick Schrock 表示:“为什么说 Ruff 改变了游戏规则?主要是因为它快了近 1000 倍。字面意思,不是笔误。在我们最大的模块(dagster 本身,25 万行代码)上,pylint 在我的 M1 芯片上使用 4 个核心并行处理需要约 2.5 分钟。而运行 ruff 检查整个代码库只需 0.4 秒。”
Bryan Van de Ven, co-creator of Bokeh, original author of Conda: Ruff is ~150-200x faster than flake8 on my machine, scanning the whole repo takes ~0.2s instead of ~20s. This is an enormous quality of life improvement for local dev. It’s fast enough that I added it as an actual commit hook, which is terrific. Timothy Crosley, creator of isort: Just switched my first project to Ruff. Only one downside so far: it’s so fast I couldn’t believe it was working till I intentionally introduced some errors. Tim Abbott, lead developer of Zulip (also here): This is just ridiculously fast… ruff is amazing.
Bokeh 的共同创建者、Conda 的原作者 Bryan Van de Ven 表示:“在我的机器上,Ruff 比 flake8 快约 150-200 倍,扫描整个仓库只需约 0.2 秒,而不是约 20 秒。这对本地开发来说是巨大的体验提升。它足够快,我已经把它添加为实际的提交钩子(commit hook),这太棒了。” isort 的创建者 Timothy Crosley 表示:“刚把我的第一个项目切换到 Ruff。目前唯一的缺点是:它太快了,以至于我直到故意引入一些错误才相信它真的在工作。” Zulip 的首席开发者 Tim Abbott 表示:“这简直快得离谱……ruff 太棒了。”
Table of Contents For more, see the documentation. Getting Started Configuration Rules Contributing Support Acknowledgements Show Your Support License Getting Started For more, see the documentation. Installation Ruff is available as ruff on PyPI. Invoke Ruff directly with uvx: uvx ruff@0.17.4 check # Lint all files in the current directory. uvx ruff@0.17.4 format # Format all files in the current directory. Or install Ruff with uv (recommended), pip, or pipx: # With uv. uv tool install ruff@latest # Install Ruff globally. uv add —dev ruff # Or add Ruff to your project. # With pip. pip install ruff # With pipx. pipx install ruff
目录:更多信息请参阅文档。入门、配置、规则、贡献、支持、致谢、展示你的支持、许可证。入门:更多信息请参阅文档。安装:Ruff 在 PyPI 上以 ruff 的名称提供。使用 uvx 直接调用 Ruff:uvx ruff@0.17.4 check(检查当前目录下的所有文件),uvx ruff@0.17.4 format(格式化当前目录下的所有文件)。或者使用 uv(推荐)、pip 或 pipx 安装 Ruff:使用 uv(uv tool install ruff@latest 全局安装,或 uv add --dev ruff 添加到你的项目),使用 pip(pip install ruff),或使用 pipx(pipx install ruff)。
Starting with version 0.5.0, Ruff can be installed with our standalone installers: # On macOS and Linux. curl -LsSf https://astral.sh/ruff/install.sh | sh # On Windows. powershell -c “irm https://astral.sh/ruff/install.ps1 | iex” # For a specific version. curl -LsSf https://astral.sh/ruff/0.17.4/install.sh | sh powershell -c “irm https://astral.sh/ruff/0.17.4/install.ps1 | iex” You can also install Ruff via Homebrew, Conda, and with a variety of other package managers.
从 0.5.0 版本开始,Ruff 可以通过我们的独立安装程序进行安装:在 macOS 和 Linux 上使用 curl -LsSf https://astral.sh/ruff/install.sh | sh;在 Windows 上使用 powershell -c "irm https://astral.sh/ruff/install.ps1 | iex"。如需特定版本,可使用 curl -LsSf https://astral.sh/ruff/0.17.4/install.sh | sh 或 powershell -c "irm https://astral.sh/ruff/0.17.4/install.ps1 | iex"。你也可以通过 Homebrew、Conda 以及其他各种包管理器安装 Ruff。
Usage To run Ruff as a linter, try any of the following: ruff check # Lint all files in the current directory (and any subdirectories). ruff check path/to/code/ # Lint all files in /path/to/code (and any subdirectories). ruff check path/to/code/.py # Lint all .py files in /path/to/code. ruff check path/to/code/to/file.py # Lint file.py. ruff check @arguments.txt # Lint using an input file, treating its contents as newline-delimited command-line arguments. Or, to run Ruff as a formatter: ruff format # Format all files in the current directory (and any subdirectories). ruff format path/to/code/ # Format all files in /path/to/code (and any subdirectories). ruff format path/to/code/.py # Format all .py files in /path/to/code. ruff format path/to/code/to/file.py # Format file.py. ruff format @arguments.txt # Format using an input file, treating its contents as newline-delimited command-line arguments.
用法:要将 Ruff 作为检查器运行,请尝试以下任一命令:ruff check(检查当前目录及子目录下的所有文件)、ruff check path/to/code/(检查指定路径下的所有文件)、ruff check path/to/code/*.py(检查指定路径下的所有 .py 文件)、ruff check path/to/code/to/file.py(检查指定文件)、ruff check @arguments.txt(使用输入文件进行检查,将其内容视为换行符分隔的命令行参数)。或者,要将 Ruff 作为格式化工具运行:ruff format(格式化当前目录及子目录下的所有文件)、ruff format path/to/code/(格式化指定路径下的所有文件)、ruff format path/to/code/*.py(格式化指定路径下的所有 .py 文件)、ruff format path/to/code/to/file.py(格式化指定文件)、ruff format @arguments.txt(使用输入文件进行格式化,将其内容视为换行符分隔的命令行参数)。
Ruff can also be used as a pre-commit hook via ruff-pre-commit: - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. rev: v0.17.4 hooks: # Run the linter. - id: ruff-check args: [ —fix ] # Run the formatter. - id: ruff-format Ruff can also be used as a VS Code extension or with various other editors. Ruff can also be used as a GitHub Action via ruff-action: name: Ruff on: [ push, pull_request ] jobs: ruff: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: astral-sh/ruff-action@v3
Ruff 还可以通过 ruff-pre-commit 用作预提交钩子(pre-commit hook):配置包括仓库地址、版本号以及运行检查器(带 —fix 参数)和格式化工具的钩子。Ruff 还可以作为 VS Code 扩展或在其他各种编辑器中使用。Ruff 还可以通过 ruff-action 作为 GitHub Action 使用,配置包括名称、触发条件(push, pull_request)以及在 ubuntu-latest 上运行的作业步骤。
Configuration Ruff can be configured through a pyproject.toml, ruff.toml, or .ruff.toml file (see: Configuration, or Settings for a complete list of all configuration options). For the complete list of enabled rules, see Default Rules. If left unspecified, Ruff’s default configuration is equivalent to the following ruff.toml file: # Exclude a variety of commonly ignored directories. exclude = [ “.bzr”, “.direnv”, “.eggs”, “.git”, “.git-rewrite”, “.hg”, “.ipynb_checkpoints”, “.mypy_cache”, “.nox”, “.pants.d”, “.pyenv”, “.pytest_cache”, “.pytype”, “.ruff_cache”, “.svn”, “.tox”, “.venv”, “.vscode”, “pypackages”, “_build”, “buck-out”, “build”, “dist”, “node_modules”, “site-packages”, “venv”, ] # Same as Black. line-length = 88 indent-width = 4 # Assume Python 3.11 target-version = “py311” [lint] # select = […] # See the Default Rules page for the full listing. ignore = [] # Allow fix for all enabled rules (when --fix) is provided. fixable = [“ALL”] unfixable = [] # Allow unused variables when unde
配置:Ruff 可以通过 pyproject.toml、ruff.toml 或 .ruff.toml 文件进行配置(请参阅“配置”或“设置”以获取所有配置选项的完整列表)。有关已启用规则的完整列表,请参阅“默认规则”。如果未指定,Ruff 的默认配置等同于以下 ruff.toml 文件:排除各种通常被忽略的目录;设置行长度为 88,缩进宽度为 4;假设目标版本为 Python 3.11;在 [lint] 部分,设置忽略列表为空,允许所有已启用规则的自动修复(当提供 —fix 时),并设置不可修复列表为空。