Ruff 是一个用 Rust 编写的高性能 Python 静态分析工具和代码格式化工具。它旨在提供快速的代码检查和格式化功能,同时支持丰富的配置选项和与现有工具的兼容性。ruff
是用rust实现的python Linter&Formatter。
安装:
conda install -c conda-forge ruff
# pip
pip install ruff
它可以作为代码检查工具和代码格式化工具使用
-
运行代码检查
ruff check .
检查并修复:
ruff check . --fix
-
运行代码格式化
ruff format .
Ruff 支持通过 pyproject.toml
文件进行配置。
创建pyproject.py
文件,并写入:
[tool.ruff]
line-length = 120
[tool.ruff.lint]
select = ["E", "F", "W"] # 启用特定的检查规则
ignore = ["W191"]
[tool.ruff.format]
quote-style = "single"
indent-style = "tab"
docstring-code-format = true
上面表示使用F,E,W
规则,忽略w191
规则。代码格式化采用单引号、缩进使用tab
等。
代码规范与美观: Python Linter (Ruff) 和 Formatter (Black) - 知乎 (zhihu.com)
大一统的 Ruff: All-in-One Linter & Formatter for Python - 知乎 (zhihu.com)
The Ruff Formatter | Ruff (astral.sh)
astral-sh/ruff: An extremely fast Python linter and code formatter, written in Rust. (github.com)