跳转到主要内容

一个极快的Python代码检查器和格式化工具,用Rust编写。

项目描述

Ruff

Ruff image image image Actions status Discord

文档 | 游乐场

一个极快的Python代码检查器和格式化工具,用Rust编写。

Shows a bar chart with benchmark results.

从头开始对CPython代码库进行代码检查。

  • ⚡️ 比现有的代码检查器(如Flake8)和格式化工具(如Black)快10-100倍
  • 🐍 通过 pip 安装
  • 🛠️ 支持 pyproject.toml
  • 🤝 Python 3.13 兼容性
  • ⚖️ 与 Flake8、isort 和 Black 兼容
  • 📦 内置缓存,避免重复分析未更改的文件
  • 🔧 支持修复,用于自动错误纠正(例如,自动删除未使用的导入)
  • 📏 超过 800 个内置规则,具有对流行的Flake8插件(如flake8-bugbear)的本地重新实现
  • ⌨️ 为 VS Code更多 提供第一方编辑器集成
  • 🌎 单一仓库友好,具有 分层和级联配置

Ruff旨在比替代工具快几个数量级,同时在单个公共接口下集成更多功能。

Ruff可以用来替代Flake8(以及数十个插件)、Blackisortpydocstylepyupgradeautoflake 等,所有这些在执行速度上比任何单个工具快数十倍或数百倍。

Ruff在Apache Airflow、Apache Superset、FastAPI、Hugging Face、Pandas、SciPy等主要开源项目中得到积极开发和使用

...以及 许多其他项目

Ruff由Astral支持。阅读启动帖子,或原始的项目公告

用户评价

Sebastián RamírezFastAPI的创建者

Ruff运行如此之快,有时我会在代码中故意添加一个有意为之的错误,只是为了确认它实际上正在运行并检查代码。

Nick SchrockElementl的创始人,GraphQL的共同创建者

为什么Ruff是一个颠覆性的工具?主要是因为它几乎快1000倍。字面上的。不是打字错误。在我们的最大模块(dagster本身,250k行代码)上,pylint大约需要2.5分钟,在我的M1上并行化在4个核心上。运行ruff对我们的整个代码库的整个代码库只需0.4秒。

Bryan Van de VenBokeh的共同创建者,Conda的原始作者

Ruff在我的机器上比flake8快150-200倍,扫描整个存储库需要约0.2秒,而不是约20秒。这对于本地开发来说是一个巨大的生活质量改进。它足够快,以至于我将其添加为实际的提交钩子,这真是太棒了。

Timothy Crosleyisort的创建者

刚刚将我的第一个项目切换到了Ruff。到目前为止只有一个缺点:它运行得太快了,我简直不敢相信它在工作,直到我故意引入了一些错误。

Tim Abbott,Zulip的负责人开发人员

这太不可思议了... ruff太棒了。

目录

更多内容,请参阅文档

  1. 入门
  2. 配置
  3. 规则
  4. 贡献
  5. 支持
  6. 致谢
  7. 谁在使用Ruff?
  8. 许可证

入门

更多内容,请参阅文档

安装

Ruff可在PyPI上的ruff中获取

# With pip.
pip install ruff

# With pipx.
pipx install ruff

从版本0.5.0开始,Ruff可以使用我们的独立安装程序进行安装

# 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.6.9/install.sh | sh
powershell -c "irm https://astral.sh/ruff/0.6.9/install.ps1 | iex"

您还可以通过HomebrewConda以及各种其他包管理器安装Ruff。

使用

要作为检查器运行Ruff,尝试以下任何一种

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.

或者,要作为格式化器运行Ruff

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还可以通过pre-commit钩子使用ruff-pre-commit

- repo: https://github.com/astral-sh/ruff-pre-commit
  # Ruff version.
  rev: v0.6.9
  hooks:
    # Run the linter.
    - id: ruff
      args: [ --fix ]
    # Run the formatter.
    - id: ruff-format

Ruff还可以作为VS Code扩展或与各种其他编辑器一起使用。

Ruff还可以通过GitHub Action使用ruff-action

name: Ruff
on: [ push, pull_request ]
jobs:
  ruff:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/ruff-action@v1

配置

Ruff可以通过pyproject.tomlruff.toml.ruff.toml文件进行配置(有关完整配置选项的列表,请参阅配置设置)。

如果未指定,Ruff的默认配置等同于以下ruff.toml文件

# 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.8
target-version = "py38"

[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`)  codes by default.
select = ["E4", "E7", "E9", "F"]
ignore = []

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

请注意,在pyproject.toml中,每个部分标题都应该以tool.ruff为前缀。例如,[lint]应替换为[tool.ruff.lint]

某些配置选项可以通过专门的命令行参数提供,例如与规则启用和禁用、文件发现和日志级别相关的选项

ruff check --select F401 --select F403 --quiet

其余的配置选项可以通过通配符--config参数提供

ruff check --config "lint.per-file-ignores = {'some_file.py' = ['F841']}"

要选择最新的检查规则、格式化器样式更改、界面更新等,请通过在配置文件中设置preview = true或在命令行上传递--preview来启用预览模式。预览模式启用了一组可能在不稳定之前发生变化的不稳定功能。

有关Ruff的顶级命令,请参阅ruff help,有关检查和格式化命令,请分别参阅ruff help checkruff help format

规则

Ruff支持超过800个检查规则,其中许多受到Flake8、isort、pyupgrade等流行工具的启发。无论规则的来源如何,Ruff都将其作为第一方功能在Rust中重新实现。

默认情况下,Ruff启用了Flake8的F规则以及E规则的子集,省略了任何与格式化器使用的样式规则重叠的规则,如ruff formatBlack

如果您刚开始使用Ruff,默认规则集是一个很好的起点:它不需要配置即可捕获各种常见错误(如未使用的导入)。

除了默认规则外,Ruff还重新实现了Flake8的一些最受欢迎的插件和相关代码质量工具,包括

有关支持的规则完整列表,请参阅规则

贡献

欢迎并高度重视贡献。要开始,请查看贡献指南

您还可以加入我们的Discord

支持

遇到问题?请查看GitHub上的现有问题,或者您可以新建一个问题

您还可以在Discord上寻求帮助。

致谢

Ruff的代码检查器借鉴了Python生态系统中的许多其他工具的API和实现细节,尤其是Flake8Pyflakespycodestylepydocstylepyupgradeisort

在某些情况下,Ruff包含了对应工具的“直接”Rust端口。我们感谢这些工具的维护者为他们的工作以及为Python社区提供的一切价值。

Ruff的格式化器建立在Rome的rome_formatter的分支之上,再次借鉴了来自RomePrettierBlack的API和实现细节。

Ruff的导入解析器基于来自Pyright的导入解析算法。

Ruff还受到Python生态系统之外的一些工具的影响,如ClippyESLint

Ruff受益于大量的贡献者

Ruff采用MIT许可证发布。

谁在使用Ruff?

Ruff被多个主要开源项目和公司使用,包括

显示您的支持

如果您使用 Ruff,请考虑将 Ruff 徽章添加到项目的 README.md

[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

...或 README.rst

.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
    :target: https://github.com/astral-sh/ruff
    :alt: Ruff

...或,作为 HTML

<a href="https://github.com/astral-sh/ruff"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff" style="max-width:100%;"></a>

许可协议

此存储库受MIT 许可协议许可

项目详情


发布历史 发布通知 | RSS 源

下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

ruff-0.6.9.tar.gz (3.1 MB 查看哈希值)

上传时间

构建分布

ruff-0.6.9-py3-none-win_arm64.whl (8.7 MB 查看哈希值)

上传时间 Python 3 Windows ARM64

ruff-0.6.9-py3-none-win_amd64.whl (9.4 MB 查看哈希值)

上传时间 Python 3 Windows x86-64

ruff-0.6.9-py3-none-win32.whl (8.5 MB 查看哈希值)

上传时间 Python 3 Windows x86

ruff-0.6.9-py3-none-musllinux_1_2_x86_64.whl (11.0 MB 查看哈希值)

上传时间 Python 3 musllinux: musl 1.2+ x86-64

ruff-0.6.9-py3-none-musllinux_1_2_i686.whl (10.6 MB 查看哈希值)

上传时间 Python 3 musllinux: musl 1.2+ i686

ruff-0.6.9-py3-none-musllinux_1_2_armv7l.whl (10.3 MB 查看哈希值)

上传时间 Python 3 musllinux: musl 1.2+ ARMv7l

ruff-0.6.9-py3-none-musllinux_1_2_aarch64.whl (10.7 MB 查看哈希值)

上传时间 Python 3 musllinux: musl 1.2+ ARM64

ruff-0.6.9-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.9 MB 查看哈希值)

上传时间: Python 3 manylinux: glibc 2.17+ x86-64

ruff-0.6.9-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl (12.5 MB 查看哈希值)

上传时间: Python 3 manylinux: glibc 2.17+ s390x

ruff-0.6.9-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (11.3 MB 查看哈希值)

上传时间: Python 3 manylinux: glibc 2.17+ ppc64le

ruff-0.6.9-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (11.8 MB 查看哈希值)

上传时间: Python 3 manylinux: glibc 2.17+ ppc64

ruff-0.6.9-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl (11.0 MB 查看哈希值)

上传时间: Python 3 manylinux: glibc 2.17+ i686

ruff-0.6.9-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (10.2 MB 查看哈希值)

上传时间: Python 3 manylinux: glibc 2.17+ ARMv7l

ruff-0.6.9-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (10.8 MB 查看哈希值)

上传时间: Python 3 manylinux: glibc 2.17+ ARM64

ruff-0.6.9-py3-none-macosx_11_0_arm64.whl (9.7 MB 查看哈希值)

上传时间: Python 3 macOS 11.0+ ARM64

ruff-0.6.9-py3-none-macosx_10_12_x86_64.whl (10.0 MB 查看哈希值)

上传时间: Python 3 macOS 10.12+ x86-64

ruff-0.6.9-py3-none-linux_armv6l.whl (10.4 MB 查看哈希值)

上传时间: Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面