Loading...
Loading...
Compare original and translation side by side
undefinedundefinedundefinedundefinedEFruff check --fixruff check --output-format githubpyproject.tomlruff.tomlEFruff check --fixruff check --output-format githubpyproject.tomlruff.tomlF401lint.selectlint.extend-selectlint.ignoreF401lint.selectlint.extend-selectlint.ignore[tool.ruff.lint]
select = ["E", "F"] # pycodestyle errors + Pyflakes[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"F", # Pyflakes
"UP", # pyupgrade
"B", # flake8-bugbear
"SIM", # flake8-simplify
"I", # isort
][tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # Pyflakes
"UP", # pyupgrade
"B", # flake8-bugbear
"SIM", # flake8-simplify
"I", # isort
"N", # pep8-naming
"S", # flake8-bandit (security)
"C4", # flake8-comprehensions
"DTZ", # flake8-datetimez
"T20", # flake8-print
"RUF", # Ruff-specific rules
]
ignore = ["E501"] # Line too long (handled by formatter)[tool.ruff.lint]
select = ["E", "F"] # pycodestyle错误 + Pyflakes规则[tool.ruff.lint]
select = [
"E", # pycodestyle错误
"F", # Pyflakes规则
"UP", # pyupgrade规则
"B", # flake8-bugbear规则
"SIM", # flake8-simplify规则
"I", # isort规则
][tool.ruff.lint]
select = [
"E", # pycodestyle错误
"W", # pycodestyle警告
"F", # Pyflakes规则
"UP", # pyupgrade规则
"B", # flake8-bugbear规则
"SIM", # flake8-simplify规则
"I", # isort规则
"N", # pep8-naming规则
"S", # flake8-bandit(安全规则)
"C4", # flake8-comprehensions规则
"DTZ", # flake8-datetimez规则
"T20", # flake8-print规则
"RUF", # Ruff专属规则
]
ignore = ["E501"] # 忽略行过长(由格式化工具处理)pyproject.toml--select--ignorepyproject.tomlpyproject.tomlpyproject.toml--select--ignorepyproject.tomlpyproject.toml[tool.ruff]
line-length = 88
target-version = "py311"
exclude = [".venv", "dist", "build", "*.pyi"]
[tool.ruff.lint]
select = ["E", "F", "UP", "B", "SIM", "I"]
ignore = ["E501"]
fixable = ["ALL"]
unfixable = []
[tool.ruff.lint.per-file-ignores]
"tests/**/*.py" = ["S101"] # Allow assert in tests
"__init__.py" = ["F401"] # Allow unused imports
"**/{tests,docs,tools}/*" = ["E402"] # Allow late imports
[tool.ruff.lint.isort]
known-first-party = ["myproject"]
[tool.ruff.lint.pydocstyle]
convention = "google"[tool.ruff]
line-length = 88
target-version = "py311"
exclude = [".venv", "dist", "build", "*.pyi"]
[tool.ruff.lint]
select = ["E", "F", "UP", "B", "SIM", "I"]
ignore = ["E501"]
fixable = ["ALL"]
unfixable = []
[tool.ruff.lint.per-file-ignores]
"tests/**/*.py" = ["S101"] # 允许在测试代码中使用assert
"__init__.py" = ["F401"] # 允许在__init__.py中存在未使用的导入
"**/{tests,docs,tools}/*" = ["E402"] # 允许在测试、文档、工具目录中延迟导入
[tool.ruff.lint.isort]
known-first-party = ["myproject"]
[tool.ruff.lint.pydocstyle]
convention = "google"line-length = 88
target-version = "py311"
[lint]
select = ["E", "F", "UP", "B", "SIM", "I"]
ignore = ["E501"]
[lint.per-file-ignores]
"tests/**/*.py" = ["S101"]line-length = 88
target-version = "py311"
[lint]
select = ["E", "F", "UP", "B", "SIM", "I"]
ignore = ["E501"]
[lint.per-file-ignores]
"tests/**/*.py" = ["S101"]| Type | Behavior | Default |
|---|---|---|
| Safe | Preserves code semantics | Enabled |
| Unsafe | May change runtime behavior | Disabled |
undefined| 类型 | 行为 | 默认设置 |
|---|---|---|
| 安全 | 保留代码语义 | 启用 |
| 不安全 | 可能改变代码运行行为 | 禁用 |
undefinedundefinedundefined[tool.ruff.lint][tool.ruff.lint]
For detailed fix safety documentation, see [references/fix_safety.md](references/fix_safety.md).
如需详细的修复安全文档,请查看[references/fix_safety.md](references/fix_safety.md)。x = 1 # noqa: F841 # Ignore specific rule
i = 1 # noqa: E741, F841 # Ignore multiple rules
x = 1 # noqa # Ignore all rules (avoid this)x = 1 # noqa: F841 # 忽略特定规则
i = 1 # noqa: E741, F841 # 忽略多条规则
x = 1 # noqa # 忽略所有规则(避免使用)undefinedundefinedundefinedundefinedundefinedundefined
For detailed suppression patterns, see [references/error_suppression.md](references/error_suppression.md).
如需详细的忽略模式,请查看[references/error_suppression.md](references/error_suppression.md)。undefinedundefinedundefinedundefined| Code | Meaning |
|---|---|
| 0 | No violations found, or all fixed |
| 1 | Violations found |
| 2 | Configuration error or internal error |
ruff check . --exit-zero # Always exit 0
ruff check . --exit-non-zero-on-fix # Exit 1 if any violations (even if fixed)| 代码 | 含义 |
|---|---|
| 0 | 未发现问题,或所有问题已修复 |
| 1 | 发现问题 |
| 2 | 配置错误或内部错误 |
ruff check . --exit-zero # 始终返回0
ruff check . --exit-non-zero-on-fix # 即使修复了问题,只要发现过问题就返回1name: Lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
with:
args: "check --output-format github"name: Lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
with:
args: "check --output-format github"repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-formatrepos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format| Issue | Solution |
|---|---|
| Rule conflicts with formatter | Ignore formatting rules (E501) when using ruff format |
| Too many violations | Start with minimal rules (E, F), expand gradually |
| Too many per-file ignores | Review rule selection, consider disabling noisy rules |
| Slow on large codebase | Ensure .venv excluded, check for recursive symlinks |
| noqa not working | Check syntax: |
| 问题 | 解决方案 |
|---|---|
| 规则与格式化工具冲突 | 使用ruff format时忽略格式化相关规则(如E501) |
| 发现过多问题 | 从基础规则(E、F)开始,逐步扩展 |
| 按文件忽略过多 | 重新审视规则选择,考虑禁用噪音规则 |
| 大型代码库运行缓慢 | 确保排除.venv目录,检查是否存在递归符号链接 |
| noqa不生效 | 检查语法: |
| Prefix | Source | Description |
|---|---|---|
| E/W | pycodestyle | Style errors/warnings |
| F | Pyflakes | Logical errors |
| B | flake8-bugbear | Common bugs |
| I | isort | Import sorting |
| UP | pyupgrade | Python version upgrades |
| SIM | flake8-simplify | Code simplification |
| N | pep8-naming | Naming conventions |
| S | flake8-bandit | Security issues |
| C4 | flake8-comprehensions | Comprehension style |
| RUF | Ruff | Ruff-specific rules |
| 前缀 | 来源 | 描述 |
|---|---|---|
| E/W | pycodestyle | 代码风格错误/警告 |
| F | Pyflakes | 逻辑错误 |
| B | flake8-bugbear | 常见bug |
| I | isort | 导入排序 |
| UP | pyupgrade | Python版本升级适配 |
| SIM | flake8-simplify | 代码简化 |
| N | pep8-naming | 命名规范 |
| S | flake8-bandit | 安全问题 |
| C4 | flake8-comprehensions | 推导式风格 |
| RUF | Ruff | Ruff专属规则 |