文章506
标签266
分类65

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

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

[tool.isort]
profile = "black"

依赖配置:

requirements.txt

-e .[all]

...

requirements-dev.txt

-r requirements.txt

# Ci
black
isort

Github Actions:

.github/workflows/ci.yaml

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/2024/08/21/Python项目Linter、Formatter和Github-Actions配置/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可