feat: add ruff lint & pre-commit#313
Conversation
This workflow will be triggered when a pull request is opened. It will then post a comment "@codecov-ai-reviewer review" to help with automated AI code reviews. It will use the `peter-evans/create-or-update-comment` action to create the comment. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
- Add .pre-commit-config.yaml with ruff formatter and linter hooks - Remove .pre-commit-config.yaml from .gitignore to track the configuration - Include standard pre-commit hooks for code quality checks
Co-authored-by: Yan Chao Mei <1653720237@qq.com> Co-authored-by: imbajin <jin@apache.org>
| runs-on: ubuntu-latest | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| python-version: ["3.10", "3.11"] | ||
|
|
||
| steps: | ||
| - name: Prepare HugeGraph Server Environment | ||
| run: | | ||
| docker run -d --name=graph -p 8080:8080 -e PASSWORD=admin hugegraph/hugegraph:1.5.0 | ||
| sleep 10 | ||
|
|
||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python ${{ matrix.python-version }} | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
|
|
||
| - name: Install uv | ||
| run: | | ||
| curl -LsSf https://astral.sh/uv/install.sh | sh | ||
| echo "$HOME/.cargo/bin" >> $GITHUB_PATH | ||
|
|
||
| - name: Cache dependencies | ||
| uses: actions/cache@v4 | ||
| with: | ||
| path: | | ||
| ~/.cache/uv | ||
| ~/nltk_data | ||
| key: ${{ runner.os }}-uv-${{ matrix.python-version }}-${{ hashFiles('**/pyproject.toml', 'uv.lock') }} | ||
| restore-keys: | | ||
| ${{ runner.os }}-uv-${{ matrix.python-version }}- | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| uv sync --extra llm --extra dev | ||
| uv run python -c "import nltk; nltk.download('stopwords'); nltk.download('punkt')" | ||
|
|
||
| - name: Run unit tests | ||
| working-directory: hugegraph-llm | ||
| env: | ||
| SKIP_EXTERNAL_SERVICES: true | ||
| run: | | ||
| uv run pytest src/tests/config/ src/tests/document/ src/tests/middleware/ src/tests/operators/ src/tests/models/ src/tests/indices/ src/tests/test_utils.py -v --tb=short | ||
|
|
||
| - name: Run integration tests | ||
| working-directory: hugegraph-llm | ||
| env: | ||
| SKIP_EXTERNAL_SERVICES: true | ||
| run: | | ||
| uv run pytest src/tests/integration/test_graph_rag_pipeline.py src/tests/integration/test_kg_construction.py src/tests/integration/test_rag_pipeline.py -v --tb=short |
Check warning
Code scanning / CodeQL
Workflow does not contain permissions
| @@ -43,6 +43,9 @@ dev = [ | |||
| "pytest~=8.0.0", | |||
| "pytest-cov~=5.0.0", | |||
| "pylint~=3.0.0", | |||
There was a problem hiding this comment.
虽然 ruff 可以替代大部分 pylint 功能,但 pylint 仍然有一些独特的检查规则(如更深入的类型推断和复杂的代码分析)。当前配置同时保留了 pylint 和 ruff:
- 如果打算用 ruff 完全替代 pylint,建议在后续 PR 中移除 pylint 依赖
- 如果需要两者并存,建议在文档中说明各自的职责
另外,注意当前代码中还有大量 # pylint: disable=... 注释,迁移到 ruff 后这些注释将失效(不会报错但也不起作用)。
| [tool.ruff.lint] | ||
| # Select a broad set of rules for comprehensive checks. | ||
| # E: pycodestyle Errors, F: Pyflakes, W: pycodestyle Warnings, I: isort | ||
| # C: flake8-comprehensions, N: pep8-naming |
There was a problem hiding this comment.
当前配置选择了多个规则集,整体配置合理。有几点建议:
-
T20(flake8-print): 这个规则在生产代码中很有用,但注意当前只在测试文件和 examples 中忽略了它。如果项目其他地方有合法的 print 使用(如 CLI 工具输出),可能需要更多的豁免配置。 -
C901(complexity): 虽然暂时忽略了复杂度检查,建议后续通过重构逐步启用,这是代码质量的重要指标。 -
缺少
D(pydocstyle): 如果项目对文档字符串有要求,可以考虑启用。 -
缺少
S(flake8-bandit): 建议启用安全相关的检查,特别是对于处理用户输入的 LLM 项目。
# 可考虑添加的安全规则
select = [..., "S"] # flake8-bandit for security|
|
||
| ## 🚀 Quick Start | ||
|
|
||
| Choose your preferred deployment method: |
There was a problem hiding this comment.
配置文件位于项目根目录 ../.pre-commit-config.yaml,但从 hugegraph-llm/ 子目录运行 pre-commit install 可能不会正确识别根目录的配置。
建议:
- 在文档中说明需要在项目根目录运行
pre-commit install - 或者验证当前配置在子目录中也能正常工作
Replaced str() with !s in f-strings for more concise formatting in multiple modules. Updated type hints to use PEP 604 syntax (e.g., dict | None) where appropriate. Made minor improvements to logging, singleton pattern, and test error reporting. Updated ruff configuration to include RUF rules and added formatting options.
Replaced unused variables with underscores in BGNNPredictor and LinkPredictionPGNN to clarify intent and prevent linter warnings.
Co-authored-by: imbajin <jin@apache.org>
No description provided.