Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ruff): add Ruff configuration for code linting and formatting #107

Open
wants to merge 5 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Ruff Linter

on: [ push, pull_request ]

jobs:
ruff:
name: Run Ruff Linter
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.x' # 兼容所有 Python 版本

- name: Cache Ruff
uses: actions/cache@v3
with:
path: ~/.cache/ruff
key: ruff-${{ runner.os }}-${{ github.sha }}
restore-keys: |
ruff-${{ runner.os }}-

- name: Run Ruff Linter
uses: astral-sh/ruff-action@v3
with:
args: check . # 执行 lint 代码检查

# 尝试自动修复
- name: Fix Ruff Issues (Optional)
run: ruff check . --fix

# 如果检测失败,则终止 CI
- name: Fail if Lint Errors Exist
run: |
if [[ $(ruff check . --output-format=json | jq '. | length') -gt 0 ]]; then
exit 1
fi
32 changes: 32 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,35 @@ dependencies = [
"restrictedpython>=8.0",
"scipy>=1.15.1",
]

# 配置 Ruff 的整体行为
[tool.ruff]
line-length = 79 # https://peps.python.org/pep-0008/

# 配置 Ruff 的代码检查功能(Linting)
[tool.ruff.lint]
# 选择要启用的规则集
select = [
"E", # PEP 8 代码格式错误(例如空格、缩进、换行等)
"F", # Pyflakes 代码检查(未使用的变量、重复导入、变量未定义等)
"W", # 一般代码风格警告(如行尾空格、缩进警告等)
"UP" # Python 版本升级检查(建议 Python 3 代码升级,如 `typing.List -> list`)
]

# 忽略特定规则,避免误报或影响开发效率
ignore = [
"E203", # `:` 前有空格(与 Black 代码格式化冲突)
"E266", # 过多的 `#` 号用于注释(有时用于代码分割)
"E501", # 行长度超过限制(Ruff 已经使用 `line-length` 处理)
"F401", # 导入未使用的模块(有时用于动态导入)
]

# 配置 Ruff 代码格式化功能(类似于 Black)
[tool.ruff.format]
quote-style = "single" # 统一使用单引号(')而非双引号("),确保代码风格一致
indent-style = "space" # 统一使用空格缩进,而不是 Tab
docstring-code-format = true # 启用docstring代码片段格式化

# 配置 Ruff 的 PyUpgrade 功能(Python 版本兼容性优化)
[tool.ruff.lint.pyupgrade]
keep-runtime-typing = true # 保留 `from __future__ import annotations`,避免影响运行时类型检查
Loading