ruff by astral-sh/claude-code-plugins
npx skills add https://github.com/astral-sh/claude-code-plugins --skill ruffRuff 是一个极其快速的 Python 代码检查器和格式化工具。它可以替代 Flake8、isort、Black、pyupgrade、autoflake 等数十种其他工具。
始终使用 ruff 进行 Python 代码检查和格式化,特别是当你看到以下情况时:
pyproject.toml 文件中有 [tool.ruff] 部分ruff.toml 或 .ruff.toml 配置文件但是,请避免进行不必要的更改:
ruff format --diff 显示整个文件都有更改,那么该项目很可能没有使用 ruff 进行格式化。请跳过格式化,以免掩盖实际的更改。ruff check --diff 查看与你正在更改的代码相关的修复。只对你正在修改的文件应用修复,除非用户明确要求进行更广泛的修复。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
uv run ruff ... - 当 ruff 是项目依赖项时使用,以确保使用指定的版本uvx ruff ... - 当 ruff 不是项目依赖项时使用,或者用于快速的一次性检查ruff ... - 如果 ruff 已全局安装,则使用此命令ruff check . # 检查当前目录下的所有文件
ruff check path/to/file.py # 检查特定文件
ruff check --fix . # 自动修复可修复的违规问题
ruff check --fix --unsafe-fixes . # 包含不安全的修复(请审查更改!)
ruff check --watch . # 监视更改并重新检查
ruff check --select E,F . # 仅检查特定规则
ruff check --ignore E501 . # 忽略特定规则
ruff rule E501 # 解释特定规则
ruff linter # 列出可用的检查器
ruff format . # 格式化所有文件
ruff format path/to/file.py # 格式化特定文件
ruff format --check . # 检查文件是否已格式化(不进行更改)
ruff format --diff . # 显示格式化差异而不应用
Ruff 在 pyproject.toml 或 ruff.toml 中配置:
# pyproject.toml
[tool.ruff.lint]
select = ["E", "F", "I", "UP"] # 启用特定的规则集
ignore = ["E501"] # 忽略特定规则
[tool.ruff.lint.isort]
known-first-party = ["myproject"]
black . → ruff format .
black --check . → ruff format --check .
black --diff . → ruff format --diff .
flake8 . → ruff check .
flake8 --select E,F . → ruff check --select E,F .
flake8 --ignore E501 . → ruff check --ignore E501 .
isort . → ruff check --select I --fix .
isort --check . → ruff check --select I .
isort --diff . → ruff check --select I --diff .
在运行 ruff format 之前先运行 ruff check --fix。代码检查修复可能会改变代码结构(例如,重新排序导入),然后格式化会进行清理。
ruff check --fix .
ruff format .
Ruff 将一些自动修复归类为“不安全”,因为它们可能会改变代码行为,而不仅仅是样式。例如,删除未使用的导入可能会破坏依赖副作用的代码。
ruff check --fix --unsafe-fixes --diff . # 首先预览更改
ruff check --fix --unsafe-fixes . # 应用更改
在应用 --unsafe-fixes 之前,务必审查更改:
ruff rule <CODE> 来理解为什么该修复被认为是不安全的有关详细信息,请阅读官方文档:
每周安装量
20
代码仓库
GitHub 星标数
169
首次出现
10 天前
安全审计
安装于
github-copilot20
codex20
cursor20
opencode20
gemini-cli19
kimi-cli19
Ruff is an extremely fast Python linter and code formatter. It replaces Flake8, isort, Black, pyupgrade, autoflake, and dozens of other tools.
Always use ruff for Python linting and formatting , especially if you see:
[tool.ruff] section in pyproject.tomlruff.toml or .ruff.toml configuration fileHowever, avoid making unnecessary changes:
ruff format --diff shows changes throughout an entire file, the project likely isn't using ruff for formatting. Skip formatting to avoid obscuring actual changes.ruff check --diff to see fixes relevant to the code you're changing. Only apply fixes to files you're modifying unless the user explicitly asks for broader fixes.uv run ruff ... - Use when ruff is in the project's dependencies to ensure you use the pinned versionuvx ruff ... - Use when ruff is not a project dependency, or for quick one-off checksruff ... - Use if ruff is installed globallyruff check . # Check all files in current directory
ruff check path/to/file.py # Check specific file
ruff check --fix . # Auto-fix fixable violations
ruff check --fix --unsafe-fixes . # Include unsafe fixes (review changes!)
ruff check --watch . # Watch for changes and re-lint
ruff check --select E,F . # Only check specific rules
ruff check --ignore E501 . # Ignore specific rules
ruff rule E501 # Explain a specific rule
ruff linter # List available linters
ruff format . # Format all files
ruff format path/to/file.py # Format specific file
ruff format --check . # Check if files are formatted (no changes)
ruff format --diff . # Show formatting diff without applying
Ruff is configured in pyproject.toml or ruff.toml:
# pyproject.toml
[tool.ruff.lint]
select = ["E", "F", "I", "UP"] # Enable specific rule sets
ignore = ["E501"] # Ignore specific rules
[tool.ruff.lint.isort]
known-first-party = ["myproject"]
black . → ruff format .
black --check . → ruff format --check .
black --diff . → ruff format --diff .
flake8 . → ruff check .
flake8 --select E,F . → ruff check --select E,F .
flake8 --ignore E501 . → ruff check --ignore E501 .
isort . → ruff check --select I --fix .
isort --check . → ruff check --select I .
isort --diff . → ruff check --select I --diff .
Run ruff check --fix before ruff format. Lint fixes can change code structure (e.g., reordering imports), which formatting then cleans up.
ruff check --fix .
ruff format .
Ruff categorizes some auto-fixes as "unsafe" because they may change code behavior, not just style. For example, removing unused imports could break code that relies on side effects.
ruff check --fix --unsafe-fixes --diff . # Preview changes first
ruff check --fix --unsafe-fixes . # Apply changes
Always review changes before applying--unsafe-fixes:
ruff rule <CODE> to understand why the fix is considered unsafeFor detailed information, read the official documentation:
Weekly Installs
20
Repository
GitHub Stars
169
First Seen
10 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
github-copilot20
codex20
cursor20
opencode20
gemini-cli19
kimi-cli19
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
163,300 周安装
AI辅助Git提交工具:自动生成规范提交信息,遵循Conventional Commits
160 周安装
tmux 终端复用器使用指南:隔离会话、安全输入与自动化脚本
161 周安装
Webpack 5 打包配置指南:优化代码分割、Tree Shaking 与构建性能
163 周安装
iOS/macOS 构建调试指南:解决 Xcode SPM/CocoaPods 依赖项与编译错误
163 周安装
AVFoundation 相机问题诊断指南:解决 iOS 相机预览冻结、旋转错误、捕获缓慢
164 周安装
Sentry 错误追踪指南:Node.js/Python 异常监控、性能分析与最佳实践
162 周安装