Ruff v0.16.0 – Significant new updates – 413 default rules up from 59

Ruff v0.16.0 – Significant new updates – 413 default rules up from 59

Ruff v0.16.0 is available now! Install it from PyPI, or with your package manager of choice: uv tool install ruff@latest As a reminder: Ruff is an extremely fast Python linter and formatter, written in Rust. Ruff can be used to replace Black, Flake8 (plus dozens of plugins), isort, pydocstyle, pyupgrade, and more, all while executing tens or hundreds of times faster than any individual tool.

Ruff v0.16.0 现已发布!你可以通过 PyPI 或你偏好的包管理器进行安装:uv tool install ruff@latest。提醒一下:Ruff 是一个用 Rust 编写的极速 Python 代码检查器(linter)和格式化工具。Ruff 可以替代 Black、Flake8(及其数十个插件)、isort、pydocstyle、pyupgrade 等工具,且执行速度比任何单一工具快几十甚至上百倍。

Migrating to v0.16

迁移至 v0.16

Ruff v0.16 has a small number of breaking changes, allowing most users to update without significant changes to code or configuration. The main exception is described below.

Ruff v0.16 包含少量破坏性变更,大多数用户无需对代码或配置进行重大修改即可升级。主要例外情况说明如下。

Better default rule set

更完善的默认规则集

Ruff now enables 413 rules by default, up from 59 in previous versions. Since Ruff’s default rule set was last modified in v0.1.0, the number of rules in Ruff has grown from 708 to 968. Many of these rules catch severe issues, including syntax errors and immediate runtime errors but were not previously enabled by default. With the new rule set, Ruff will bring these issues and many others to your attention without any Ruff configuration.

Ruff 现在默认启用 413 条规则,高于之前版本的 59 条。自 Ruff 在 v0.1.0 版本最后一次修改默认规则集以来,其内置规则总数已从 708 条增加到 968 条。其中许多规则能够捕获严重问题,包括语法错误和即时运行时错误,但此前并未默认启用。通过新的规则集,Ruff 无需任何额外配置即可提醒你注意这些问题及其他许多潜在隐患。

Even if you’re already using select or extend-select, we hope that this will draw your attention to helpful rules that you previously hadn’t discovered. The full listing of enabled rules is too long to include here, but you can find it on our new Default Rules page in the documentation. A few of the highlights include rules from the popular flake8-bugbear (B) and pyupgrade (UP) linters, as well as rules from our own RUF category.

即使你已经在配置中使用 selectextend-select,我们也希望这能让你注意到以前未曾发现的实用规则。已启用规则的完整列表篇幅过长,无法在此一一列出,但你可以在文档中新的“默认规则(Default Rules)”页面找到它们。其中的亮点包括来自流行的 flake8-bugbear (B) 和 pyupgrade (UP) 检查器的规则,以及我们自有的 RUF 类别规则。

If you want to revert to the old default set, you can easily select the old rules with this configuration:

[lint]
select = ["E4", "E7", "E9", "F"]

We view this work as closely tied to our longstanding goal of rule recategorization, so look forward to upcoming developments in this area.

如果你想恢复到旧的默认规则集,可以通过以下配置轻松选择旧规则:

[lint]
select = ["E4", "E7", "E9", "F"]

我们将此项工作视为实现长期规则重分类目标的重要一步,敬请期待该领域后续的进展。

New features in v0.16

v0.16 中的新功能

Ruff v0.16 also includes several newly stabilized features that are highlighted below.

Ruff v0.16 还包含几项新稳定的功能,重点介绍如下。

Markdown code block formatting

Markdown 代码块格式化

Ruff can now format Python code blocks embedded in Markdown files. In these files, Ruff v0.16 will format fenced code blocks with a python, py, python3, py3, pyi, or pycon info string. pyi blocks are formatted like stub files, pycon blocks as REPL sessions, and the others use normal Python file formatting.

Ruff 现在可以格式化嵌入在 Markdown 文件中的 Python 代码块。在这些文件中,Ruff v0.16 会格式化带有 pythonpypython3py3pyipycon 标识符的围栏代码块。pyi 块将按存根文件(stub files)格式化,pycon 块按 REPL 会话格式化,其余则使用标准的 Python 文件格式化规则。

This can also be used to format Quarto notebooks because Ruff still recognizes the language when surrounded by curly braces (e.g. ````{python}). Note that you may need to configure your extension mapping if your Quarto files use the .qmd` extension.

此功能也可用于格式化 Quarto 笔记本,因为 Ruff 即使在语言标识符被花括号包围时(例如 ````{python}) 也能识别。请注意,如果你的 Quarto 文件使用 .qmd` 扩展名,你可能需要配置扩展名映射。

If you need to suppress formatting, you have several options. If you want the suppression comments to appear in the code block, you can use normal fmt: off and fmt: on comments within the code block itself, or you can use similar HTML comments to disable formatting for a whole region of the document. To suppress Markdown formatting entirely, you can use the normal extend-exclude setting to exclude all Markdown files with a glob like *.md. See the full documentation for more details.

如果你需要禁止格式化,有几种选择。如果你希望禁止格式化的注释出现在代码块中,可以在代码块内部使用标准的 fmt: offfmt: on 注释;或者,你可以使用类似的 HTML 注释来禁用文档中整个区域的格式化。若要完全禁止 Markdown 格式化,可以使用标准的 extend-exclude 设置,通过类似 *.md 的 glob 模式排除所有 Markdown 文件。详情请参阅完整文档。

ruff: ignore comments offer new suppression features

ruff: ignore 注释提供新的抑制功能

Ruff now has its own suppression comment format that can be used on its own line. In v0.15, the Ruff linter gained a range suppression mechanism through paired ruff: disable and ruff: enable comments. Ruff v0.16 builds on this to add two additional ruff suppression comments. ruff: ignore can be used to suppress a diagnostic on the same line, like noqa, or on the following logical line. ruff: file-ignore comments can be used to suppress diagnostics for the entire file, just like ruff: noqa comments.

Ruff 现在拥有自己的抑制注释格式,可以单独占用一行。在 v0.15 中,Ruff 检查器通过成对的 ruff: disableruff: enable 注释引入了范围抑制机制。Ruff v0.16 在此基础上增加了两种额外的抑制注释。ruff: ignore 可用于抑制同一行上的诊断信息(类似于 noqa),或抑制下一逻辑行上的诊断。ruff: file-ignore 注释则可用于抑制整个文件的诊断信息,就像 ruff: noqa 注释一样。

As this example also shows, each of these comment kinds can have an associated “reason” explaining why they were added. ruff: ignore comments can be added automatically with the new --add-ignore CLI flag, and in preview, all of these ruff suppression comments support rule names instead of codes.

正如示例所示,每种注释类型都可以关联一个“原因(reason)”,解释添加该注释的理由。ruff: ignore 注释可以通过新的 --add-ignore 命令行标志自动添加;在预览模式下,所有这些 Ruff 抑制注释都支持使用规则名称而非规则代码。

Fixes are now shown in check and format —check output

修复建议现已显示在 checkformat --check 的输出中

Ruff now shows the diff for linter and formatter fixes when rendering diagnostics. Both the check and format subcommands have long supported the --diff flag for showing the changes introduced by applying fixes, but this was separate from the normal output and suppressed the accompanying explanatory diagnostics. In v0.16, available fixes are now shown as part of the default full output format, rendered below the help subdiagnostic.

Ruff 现在在呈现诊断信息时会显示检查器和格式化工具的修复差异(diff)。checkformat 子命令长期支持 --diff 标志以显示应用修复后的变更,但这与常规输出是分开的,且会抑制随附的解释性诊断信息。在 v0.16 中,可用的修复建议现在作为默认完整输出格式的一部分显示,位于帮助子诊断信息下方。

format --check 也支持检查器所支持的全套输出格式。你可以利用这一点获取机器可读的 JSON 输出,或生成 GitHub 和 GitLab CI 所需的格式以渲染注释。有关支持格式的完整列表,请参阅 CLI 帮助或文档。关于输出格式的最后一点说明:v0.16 对 JSON 输出有少量破坏性变更。