Python项目Linter、Formatter和Github-Actions配置
关于Python项目的一些配置问题,包括:
- Linter
- Formatter
- Github-Actions配置;
源代码:
Linter
Linter 使用的是 PyCharm 自带的,符合 PEP8 规范;
也可以使用:
直接用 isort 和 black 就可以了:
项目配置
Pre-Commit:
.pre-commit-config.yaml
1 2 3 4 5 6 7 8 9
| repos: - repo: https://github.com/psf/black rev: 23.1.0 hooks: - id: black - repo: https://github.com/pycqa/isort rev: 5.12.0 hooks: - id: isort
|
PyProject:
pyproject.toml
1 2
| [tool.isort] profile = "black"
|
依赖配置:
requirements.txt
requirements-dev.txt
1 2 3 4 5
| -r requirements.txt
# Ci black isort
|
Github Actions:
.github/workflows/ci.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| name: CI
on: workflow_dispatch: push: pull_request:
jobs: lint_and_test: runs-on: ubuntu-latest strategy: max-parallel: 4 matrix: python-version: ['3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} id: setup_python uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} cache: 'pip' cache-dependency-path: 'requirements-dev.txt' - name: Install dependencies run: | pip install -r requirements-dev.txt - name: Run lint uses: pre-commit/action@v2.0.0
|
附录
源代码:
参考: