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真是太神奇了。
目录
有关更多信息,请参阅文档。
- 入门
- 配置
- 规则
- 贡献
- 支持
- 致谢
- 谁在使用ruff ?
- 执照
入门
有关更多信息,请参阅文档。
安装
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许可证获得许可的
