文章524
标签277
分类67

Python项目Linter、Formatter和Github-Actions配置

关于Python项目的一些配置问题,包括:

  • Linter
  • Formatter
  • Github-Actions配置;

源代码:


Python项目Linter、Formatter和Github-Actions配置

Linter

Linter 使用的是 PyCharm 自带的,符合 PEP8 规范;

也可以使用:

  • flake8
  • pylint

Formatter

直接用 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

1
2
3
-e .[all]

...

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

附录

源代码:

参考:



本文作者:Jasonkay
本文链接:https://jasonkayzk.github.io/cn/2024/08/21/Python%E9%A1%B9%E7%9B%AELinter%E3%80%81Formatter%E5%92%8CGithub-Actions%E9%85%8D%E7%BD%AE/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可