ruff
官方文档:Tutorial - Ruff
推荐使用 ruff 安装
uv tool install ruff
常用命令
- ruff check
- ruff format
Configuration
Ruff 会在文件目录或任何父目录中查找第一个 pyproject.toml
、 ruff.toml
或 .ruff.toml
文件作为配置文件
默认情况下,Ruff 启用了 Flake8 的 F
规则,以及 E
规则的一个子集,省略了与格式化工具(如 ruff format
或 Black)重叠的任何样式规则。
If you're introducing a linter for the first time, the default rule set is a great place to start: it's narrow and focused while catching a wide variety of common errors (like unused imports) with zero configuration.
Ignoring Errors
任何 lint 规则都可以通过在相关行添加 # noqa
注释来忽略。例如,让我们忽略 Iterable
导入的 UP035
规则:
from typing import Iterable # noqa: UP035
def sum_even_numbers(numbers: Iterable[int]) -> int:
"""Given an iterable of integers, return the sum of all even numbers in the iterable."""
return sum(num for num in numbers if num % 2 == 0)
如果我们想在整个文件中忽略某条规则,可以在文件的任意位置添加 # ruff: noqa: {code}
,最好靠近顶部,如下所示:
# ruff: noqa: UP035
from typing import Iterable
def sum_even_numbers(numbers: Iterable[int]) -> int:
"""Given an iterable of integers, return the sum of all even numbers in the iterable."""
return sum(num for num in numbers if num % 2 == 0)
Adding Rules
在现有代码库上启用新规则时,您可能希望忽略该规则的所有现有违规行为,转而专注于今后执行该规则。
Ruff 通过 --add-noqa
标志启用此工作流程,该标志将根据现有违规情况为每行添加 # noqa
指令。我们可以将 --add-noqa
与 --select
命令行标志结合使用,为所有现有的 UP035
违规添加 # noqa
指令:
uv run ruff check --select UP035 --add-noqa .
Integrations
Ruff 也可以通过 ruff-pre-commit
作为预提交钩子(pre-commit hook)使用
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.9.6
hooks:
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
Ruff Linter
Ruff Linter 是一款极其快速的 Python 代码检查工具,旨在作为 Flake8(及其众多插件)、isort、pydocstyle、pyupgrade、autoflake 等工具的替代品
ruff check
是 Ruff 代码检查工具的主要入口点。它接受文件或目录列表,并对所有发现的 Python 文件进行代码检查,可选择修复任何可修复的错误。当检查目录时,Ruff 会递归地搜索该目录及其所有子目录中的 Python 文件:
ruff check # Lint files in the current directory.
ruff check --fix # Lint files in the current directory and fix any fixable errors.
ruff check --watch # Lint files in the current directory and re-lint on change.
ruff check path/to/code/ # Lint files in `path/to/code`.
Rule selection
启用的规则集通过 lint.select
、 lint.extend-select
和 lint.ignore
设置进行控制。
Ruff 的 linter 遵循了 Flake8 的规则代码系统,其中每个规则代码由一个至三个字母的前缀和三位数字组成(例如, F401
)。前缀表示规则的“来源”(例如, F
代表 Pyflakes, E
代表 pycodestyle, ANN
代表 flake8-annotations)。
规则选择器如 lint.select
和 lint.ignore
接受完整的规则代码(例如, F401
)或任何有效的前缀(例如, F
)。例如,给定以下配置文件:
[tool.ruff.lint]
select = ["E", "F"]
ignore = ["F401"]
Ruff 将启用所有带有 E
(pycodestyle)或 F
(Pyflakes)前缀的规则,但 F401
除外。
一个启用一些最受欢迎规则(而不过于迂腐)的配置可能如下所示:
[tool.ruff.lint]
select = [
# pycodestyle
"E",
# Pyflakes
"F",
# pyupgrade
"UP",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
]
Fixes
Ruff 支持自动修复多种 lint 错误。例如,Ruff 可以移除未使用的导入、重新格式化文档字符串、将类型注释重写为使用更新的 Python 语法等。
ruff check --fix
默认情况下,Ruff 会修复所有提供安全修复的违规行为(for which safe fixes are available);要确定某个规则是否支持修复,请参阅规则部分。
Fix safety
Ruff 将修复标记为“安全”和“不安全”。应用安全修复时,代码的含义和意图将保持不变,但应用不安全修复时,含义可能会发生变化。
具体而言,不安全的修复可能导致运行时行为的改变、注释的删除或两者兼有,而安全的修复旨在保留运行时行为,并且仅在删除整个语句或表达式时(例如,删除未使用的导入)才会移除注释。
Ruff 默认仅启用安全修复。通过在配置文件中设置 unsafe-fixes
或向 ruff check
传递 --unsafe-fixes
标志,可以启用不安全修复。
# Show unsafe fixes
ruff check --unsafe-fixes
# Apply unsafe fixes
ruff check --fix --unsafe-fixes
默认情况下,Ruff will display a hint when unsafe fixes are available but not enabled. 可以通过将 unsafe-fixes
设置设为 false
或使用 --no-unsafe-fixes
标志来关闭该建议。
可以通过 lint.extend-safe-fixes
和 lint.extend-unsafe-fixes
设置来调整是否启用/不启用具体某个规格的 Fix safety
[tool.ruff.lint]
extend-safe-fixes = ["F601"]
extend-unsafe-fixes = ["UP034", "E"]
Disabling fixes
要限制 Ruff 应修复的规则集,请使用 lint.fixable
或 lint.extend-fixable
以及 lint.unfixable
设置。
例如,以下配置将启用除 unused-imports
( F401
)之外的所有规则的修复:
[tool.ruff.lint]
fixable = ["ALL"]
unfixable = ["F401"]
相反,以下配置将仅启用对 F401
的修复:
[tool.ruff.lint]
fixable = ["F401"]