ruff

2025-12-11 0 815

ruff

文档|操场

一个非常快速的Python Linter和Code Formatter,用Rust编写。

显示带有基准结果的条形图。

从头开始覆盖CPYTHON代码库。

  • ⚡️比现有衬里(如片状)和格式化(如黑色)快10-100倍
  • ?可通过PIP安装
  • pyproject.toml支持
  • ?python 3.13兼容性
  • ⚖️与Flake8,Isort和黑色的偶数均等
  • ?内置缓存,以避免重新分析未更改的文件
  • ?修复支持,用于自动错误校正(例如,自动删除未使用的导入)
  • ?超过800个内置规则,具有流行的片状插件的本机重新实现,例如flake8-bugbear
  • ⌨️vs代码的第一方编辑器集成以及更多
  • ?monorepo友好,具有层次结构和级联配置

ruff目标是比替代工具更快的数量级,同时在单个公共接口背后集成了更多功能。

ruff可用于替换Flake8(加上数十个插件),黑色,ISORT,PYDOCSTYLE,PYUPGRADE,PYUPGRADE,AUTOFLAKE等,同时比任何单独的工具都要执行数十或数百次。

ruff非常积极地开发和用于主要开源项目,例如:

  • Apache气流
  • Apache超集
  • Fastapi
  • 拥抱脸
  • 熊猫
  • Scipy

…还有更多。

ruff由星体支持。阅读发布帖子或原始项目公告。

推荐

Fastapi的创建者SebastiánRamírez

ruff是如此之快,有时我在代码中添加故意错误只是为了确认其实际运行并检查代码。

GraphQl的共同创建者Elementl的创始人Nick Schrock

为什么ruff是Gamechanger?主要是因为它速度快了近1000倍。字面上地。不是错字。在我们最大的模块(达格斯特本身,250k LOC)上,皮肤大约需要2.5分钟,在我的M1上的4个核心上平行。在我们的整个代码库上运行ruff需要0.4秒。

Bokeh的共同创建者Bryan van de Ven ,Conda的原始作者:

ruff比我的机器上的flake8快〜150-200x,扫描整个仓库的时间〜0.2s而不是〜20s。对于当地开发人员来说,这是一种巨大的生活质量。它足够快,我将其添加为实际的提交挂钩,这是很棒的。

Isort的创建者Timothy Crosley

刚将我的第一个项目切换到ruff 。到目前为止,只有一个缺点:这是如此之快,我简直不敢相信它在有意提出一些错误之前起作用。

Zulip的首席开发商Tim Abbott

这简直是​​荒谬的… ruff真是太神奇了。

目录

有关更多信息,请参阅文档。

  1. 入门
  2. 配置
  3. 规则
  4. 贡献
  5. 支持
  6. 致谢
  7. 谁在使用ruff ?
  8. 执照

入门

有关更多信息,请参阅文档。

安装

ruff可作为pypi上的ruff 。

直接使用UVX调用ruff :

ruff check # Lint all files in the current directory.
uvx ruff format # Format all files in the current directory.\”>

uvx ruff check   # Lint all files in the current directory.
uvx ruff format  # Format all files in the current directory.

或使用紫外线(推荐),pip或pipx安装ruff :

ruff globally.
uv add –dev ruff # Or add ruff to your project.

# With pip.
pip install ruff

# With pipx.
pipx install ruff \”>

 # With uv.
uv tool install ruff @latest  # Install ruff globally.
uv add --dev ruff            # Or add ruff to your project.

# With pip.
pip install ruff

# With pipx.
pipx install ruff

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

 # On macOS and Linux.
curl -LsSf https://as*tral.s*h*/ruff/install.sh | sh

# On Windows.
powershell -c \" irm https://a*str*a*l.sh/ruff/install.ps1 | iex \"

# For a specific version.
curl -LsSf https://a*st*ral*.sh/ruff/0.12.9/install.sh | sh
powershell -c \" irm https://ast**ral*.sh/ruff/0.12.9/install.ps1 | iex \"

您还可以通过Homebrew,Conda以及其他各种包装管理人员安装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 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 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也可以通过ruff -pre-commit用作预先承诺的钩子:

ruff version.
rev: v0.12.9
hooks:
# Run the linter.
– id: ruff -check
args: [ –fix ]
# Run the formatter.
– id: ruff -format\”>

- repo : https://gith**ub.*com/astral-sh/ruff-pre-commit
  # ruff version.
  rev : v0.12.9
  hooks :
    # Run the linter.
    - id : ruff -check
      args : [ --fix ]
    # Run the formatter.
    - id : ruff -format

ruff也可以用作VS代码扩展名或与其他各种编辑器一起使用。

ruff也可以通过ruff -action用作github动作:

ruff
on: [ push, pull_request ]
jobs:
ruff :
runs-on: ubuntu-latest
steps:
– uses: actions/checkout@v4
– uses: astral-sh/ ruff -action@v3\”>

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

配置

可以通过ruff , ruff .toml或。 ruff .toml文件(请参阅:配置设置所有配置选项的完整列表)。

如果未指定的剩余,则ruff的默认配置等效于以下ruff .toml文件:

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.9
target-version = \”py39\”

[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\”\”>

 # 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.9
target-version = \" py39 \"

[ 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中,每个截面都应以工具为前缀。 ruff 。例如,[棉布]应替换为[工具。 ruff .lint]。

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

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

剩余的配置选项可以通过catch all-config参数提供:

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

要选择最新的棉绒规则,格式化样式更改,接口更新等,请通过在配置文件中设置preview = true或在命令行上通过-Preview来启用预览模式。预览模式可实现在稳定之前可能会改变的不稳定功能的集合。

有关ruff的顶级命令的更多信息,请参见ruff帮助,或者ruff帮助检查和ruff帮助格式,以了解有关伸长和格式化命令的更多信息。

规则

ruff支持超过800个棉绒规则,其中许多是受到Flake8,Isort,Pyupgrade等流行工具的启发。不管规则的起源如何, ruff重新实现了Rust中的每个规则作为第一方特征。

默认情况下, ruff启用Flake8的F规则以及E规则的子集,省略了与使用式格式的任何风格规则,例如ruff格式或黑色。

如果您刚刚开始使用ruff ,则默认规则集是一个很好的起点:它会捕获具有零配置的各种常见错误(如未使用的导入)。

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

  • autoflake
  • 根除
  • Flake8-2020
  • Flake8通用
  • Flake8-Async
  • Flake8-Bandit(#1646)
  • flake8盲目的外观
  • Flake8-boolean-trap
  • Flake8-bugbear
  • Flake8-Builtins
  • Flake8-Commas
  • 薄片经验
  • Flake8-opyright
  • flake8-datetimez
  • Flake8-Debugger
  • flake8-django
  • Flake8-Docstrings
  • 薄片8映射
  • flake8-errmsg
  • flake8-recutable
  • Flake8未通道通道
  • flake8-getText
  • flake8-implicit-str-concat
  • Flake8 Import-Conventions
  • Flake8-loging
  • Flake8-Goging-Format
  • Flake8-NO-PEP420
  • flake8-pie
  • Flake8-Print
  • flake8-pyi
  • Flake8-Pytest风格
  • Flake8-Quotes
  • Flake8-raise
  • Flake8-Return
  • 薄片
  • flake8修剪
  • Flake8-Slots
  • 薄片8-super
  • Flake8-tidy-imports
  • Flake8-todos
  • Flake8型检查
  • flake8-use-pathlib
  • Flynt(#2102)
  • Isort
  • 麦凯布
  • Pandas-Vet
  • pep8命名
  • PydocStyle
  • pygrep钩
  • pylint-airflow
  • pyupgrade
  • 训练
  • Yesqa

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

贡献

欢迎捐款和高度赞赏。首先,请查看贡献指南

您也可以加入我们的不和谐

支持

遇到麻烦?在Github上查看现有问题,或者随时打开一个新问题

您也可以在不和谐方面寻求帮助。

致谢

ruff的Linter借鉴了Python生态系统中许多其他工具的API和实现细节,尤其是Flake8,Pyflakes,PyCodestyle,Pydocstyle,Pydocstyle,Pyupgrade和Isort。

在某些情况下, ruff包括相应工具的“直接”生锈端口。我们感谢这些工具的维护者的工作,并感谢他们提供给Python社区的所有价值。

ruff的Formatter建立在罗马的Rome_formatter的叉子上,并再次借鉴了罗马,漂亮和黑色的API和实现细节。

ruff的导入解析器基于Pyright的导入分辨率算法。

ruff还受Python生态系统之外的许多工具的影响,例如Clippy和Eslint。

ruff是许多贡献者的受益者。

ruff根据MIT许可发布。

谁在使用ruff ?

ruff由许多主要的开源项目和公司使用,包括:

  • 符号
  • 亚马逊(AWS Sam)
  • 安基
  • 人类(Python SDK)
  • Apache气流
  • 阿斯利康(马格努斯)
  • 别贝
  • 板凳(repac)
  • 散乱
  • Capital One(Datacompy)
  • 人群(数字)
  • 密码学(PYCA)
  • CERN(INDICO)
  • DVC
  • 匕首
  • 达格斯特
  • Databricks(MLFlow)
  • Dify
  • Fastapi
  • 戈多
  • Gradio
  • 巨大的期望
  • httpx
  • 孵化
  • 家庭助理
  • 拥抱面(变形金刚,数据集,扩散器)
  • IBM(Qiskit)
  • ING银行(Popmon,Probatus)
  • ibis
  • 常春藤
  • JAX
  • Jupyter
  • Kraken Tech
  • Langchain
  • Litestar
  • Llamaindex
  • 矩阵(突触)
  • 大型
  • Meltano(Meltano Cli,Singer SDK)
  • Microsoft(语义内核,ONNX运行时,LightGBM)
  • 现代财政部(Python SDK)
  • Mozilla(Firefox)
  • mypy
  • Nautobot
  • Netflix(调度)
  • 诺基亚
  • 无机器人
  • numpyro
  • onnx
  • OpenBB
  • 开放葡萄酒组件
  • PDM
  • 桨板
  • 熊猫
  • 枕头
  • 方面
  • POSTHOG
  • 州长(Python SDK,Marvin)
  • Pyinstaller
  • pymc
  • PYMC营销
  • pytest
  • Pytorch
  • pydantic
  • 皮层
  • Pyvista
  • 反射
  • 波纹
  • 罗宾
  • Saleor
  • 缩放AI(启动SDK)
  • Scipy
  • 雪花(雪花)
  • 狮身人面像
  • 稳定的基线3
  • 小星
  • 简化
  • 算法
  • Vega-Altair
  • 魏布拉特
  • WordPress(openverse)
  • Zenml
  • Zulip
  • 构建(PYPA)
  • cibuildwheel(PYPA)
  • delta-rs
  • 特色
  • 梅森·派森
  • NOX
  • pip

表示您的支持

如果您使用的是ruff ,请考虑将ruff徽章添加到您的项目的readme.md中:

 [ ![ ruff ] ( https://img.s*h*i*elds.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json )] ( https://gith**ub.c*om/astral-sh/ruff )

…或readme.rst:

.. image :: https://img.s*h*i*elds.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
    :target: https://gith**ub.c*om/astral-sh/ruff
    :alt: ruff

…或者,如html:

 < a href =\" https://gith**ub.c*om/astral-sh/ruff \" > < img src =\" https://img.s*h*i*elds.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json \" alt =\" ruff \" style =\" max-width:100%; \" > </ a > 

执照

该存储库是根据MIT许可证获得许可的

下载源码

通过命令行克隆项目:

git clone https://github.com/astral-sh/ruff.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 ruff https://www.zuozi.net/34470.html

fastapi
上一篇: fastapi
celery
下一篇: celery
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务