astral-sh/ruff-pre-commit
GitHub: astral-sh/ruff-pre-commit
将高性能 Python 代码检查与格式化工具 Ruff 集成到 Git pre-commit 工作流中,实现提交前自动化的代码质量把控。
Stars: 1886 | Forks: 99
# ruff-pre-commit
[](https://github.com/astral-sh/ruff)
[](https://pypi.python.org/pypi/ruff)
[](https://pypi.python.org/pypi/ruff)
[](https://pypi.python.org/pypi/ruff)
[](https://github.com/astral-sh/ruff-pre-commit/actions)
一个用于 [Ruff](https://github.com/astral-sh/ruff) 的 [pre-commit](https://pre-commit.com/) 钩子。
作为独立仓库分发,以支持通过预构建的 wheel 从 [PyPI](https://pypi.org/project/ruff/) 安装 Ruff。
### 在 pre-commit 中使用 Ruff
要通过 pre-commit 运行 Ruff 的 [linter](https://docs.astral.sh/ruff/linter) 和 [formatter](https://docs.astral.sh/ruff/formatter)(自 Ruff v0.0.289 起可用),请将以下内容添加到您的 `.pre-commit-config.yaml` 中:
```
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.15.12
hooks:
# Run the linter.
- id: ruff-check
# Run the formatter.
- id: ruff-format
```
要启用 lint 修复,请将 `--fix` 参数添加到 lint 钩子中:
```
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.15.12
hooks:
# Run the linter.
- id: ruff-check
args: [ --fix ]
# Run the formatter.
- id: ruff-format
```
要避免在 Jupyter Notebook 上运行,请从允许的文件类型列表中移除 `jupyter`:
```
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.15.12
hooks:
# Run the linter.
- id: ruff-check
types_or: [ python, pyi ]
args: [ --fix ]
# Run the formatter.
- id: ruff-format
types_or: [ python, pyi ]
```
要对 `pyproject.toml` 进行 lint,请将 `pyproject` 添加到允许的文件类型列表中(需要 `identify>=2.6.18`):
```
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.15.12
hooks:
# Run the linter.
- id: ruff-check
types_or: [ python, pyi, jupyter, pyproject ]
args: [ --fix ]
# Run the formatter.
- id: ruff-format
types_or: [ python, pyi, jupyter ]
```
当使用 `--fix` 运行时,Ruff 的 lint 钩子应该放在 Ruff 的 formatter 钩子_之前_,并且放在 Black、isort 和其他格式化工具_之前_,因为 Ruff 的修复行为可能会输出需要重新格式化的代码更改。
当不使用 `--fix` 运行时,Ruff 的 formatter 钩子可以放在 Ruff 的 lint 钩子之前或之后。
(只要您的 Ruff 配置避免了任何 [linter-formatter 不兼容问题](https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules),`ruff format` 就不应引入新的 lint 错误,因此在 `ruff check --fix` _之后_运行 Ruff 的 format 钩子是安全的。)
### 在 prek 中使用 Ruff
如果您更喜欢使用 [prek](https://github.com/j178/prek) 而不是 pre-commit,您可以定义一个包含您的钩子的 `prek.toml` 文件。以下是一个等效于 `.pre-commit-config.yaml` 配置的示例:
```
[[repos]]
repo = "https://github.com/astral-sh/ruff-pre-commit"
rev = "v0.15.0" # Ruff version.
hooks = [
# Run the linter.
{ id = "ruff-check", args = ["--fix"], types_or = ["python", "pyi"] },
# Run the formatter.
{ id = "ruff-format", types_or = ["python", "pyi"] },
]
```
有关使用 `--fix` 时钩子顺序的指导,请参阅上面关于 pre-commit 的部分。
## 许可证
ruff-pre-commit 根据以下任一许可证授权:
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) 或 )
- MIT license ([LICENSE-MIT](LICENSE-MIT) 或 )
由您选择。
除非您明确声明,否则您有意提交以包含在 ruff-pre-commit 中的任何贡献,根据 Apache-2.0 许可证的定义,均应按上述方式进行双重许可,无需任何附加条款或条件。
标签:Astral, DNS解析, Formatter, Git Hook, Linter, pptx, pre-commit, pyproject.toml, Python, Ruff, 代码格式化, 代码规范, 可视化界面, 威胁情报, 开发效率, 开发者工具, 开源项目, 无后门, 网络可观测性, 逆向工具, 错误基检测, 静态代码分析