Skip to content

Commit f506109

Browse files
authored
Merge pull request #71 from nox1134/issue-68
Use pre-commit CI to automatically fix code style issues
2 parents 330a8f4 + f1439a4 commit f506109

File tree

4 files changed

+117
-38
lines changed

4 files changed

+117
-38
lines changed

.github/workflows/python_static_analysis.yml

-37
This file was deleted.

.github/workflows/static_analysis.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Static Analysis
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
workflow_dispatch:
8+
9+
jobs:
10+
job:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
15+
# https://github.com/actions/setup-python
16+
- name: Install Python 3.11
17+
uses: actions/setup-python@v5
18+
with:
19+
python-version: '3.11'
20+
21+
- name: Install Python dependencies
22+
run: |
23+
pip install --upgrade pip
24+
pip install black 'black[jupyter]' flake8 isort
25+
pip install pre-commit
26+
27+
# https://github.com/actions/checkout
28+
- name: Checkout quantifying
29+
uses: actions/checkout@v4
30+
with:
31+
path: quantifying
32+
33+
- name: pre-commit
34+
run: pre-commit run --show-diff-on-failure --color=always --all-files
35+
working-directory: ./quantifying

.pre-commit-config.yaml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
default_language_version:
4+
python: python3.11
5+
6+
ci:
7+
autofix_commit_msg: |
8+
[pre-commit.ci] auto fixes from pre-commit.com hooks
9+
10+
for more information, see https://pre-commit.ci
11+
autofix_prs: true
12+
autoupdate_branch: ''
13+
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
14+
autoupdate_schedule: weekly
15+
skip: []
16+
submodules: false
17+
18+
repos:
19+
20+
- repo: https://github.com/pre-commit/pre-commit-hooks
21+
rev: v4.1.0
22+
hooks:
23+
- id: check-added-large-files
24+
- id: check-ast
25+
- id: check-case-conflict
26+
- id: check-docstring-first
27+
- id: check-executables-have-shebangs
28+
- id: check-json
29+
- id: check-merge-conflict
30+
- id: check-toml
31+
- id: check-yaml
32+
- id: destroyed-symlinks
33+
#- id: end-of-file-fixer
34+
#- id: mixed-line-ending
35+
# args:
36+
# - --fix=no
37+
#- id: trailing-whitespace
38+
39+
- repo: local
40+
hooks:
41+
- id: black
42+
name: "Black"
43+
entry: black
44+
language: system
45+
types: ["file", "python", "text"]
46+
47+
- id: flake8
48+
name: "Flake8"
49+
entry: flake8
50+
language: system
51+
types: ["file", "python", "text"]
52+
53+
- id: isort
54+
name: "isort"
55+
entry: isort --filter-files
56+
language: system
57+
types: ["file", "python", "text"]

README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ follow these steps:
7676
7777
### Static analysis
7878
79+
#### Using [`pre-commit`][pre-commit]
80+
1. Install pre-commit
81+
82+
- Using pip:
83+
```shell
84+
pip install pre-commit
85+
```
86+
- Using homebrew:
87+
```shell
88+
brew install pre-commit
89+
```
90+
91+
2. Install the git hook scripts
92+
```shell
93+
pre-commit install
94+
```
95+
96+
It will run on every commit automatically.
97+
98+
#### Using [`dev/tools.sh`][tools-sh] helper script
7999
The [`dev/tools.sh`][tools-sh] helper script runs the static analysis tools
80100
(`black`, `flake8`, and `isort`):
81101
```shell
@@ -89,6 +109,7 @@ directories to check:
89109
```
90110
91111
[tools-sh]: /dev/tools.sh
112+
[pre-commit]: https://pre-commit.com/
92113
93114
94115
### Resources
@@ -100,12 +121,15 @@ directories to check:
100121
- [isort][isort]: _A Python utility / library to sort imports_
101122
- (It doesn't import any libraries, it only sorts and formats them.)
102123
- [ppypa/pipenv][pipenv]: _Python Development Workflow for Humans._
124+
- [pre-commit][pre-commit]: _A framework for managing and maintaining
125+
multi-language pre-commit hooks._
103126

104127
[ccospyguide]: https://opensource.creativecommons.org/contributing-code/python-guidelines/
105128
[black]: https://github.com/psf/black
106129
[flake8]: https://gitlab.com/pycqa/flake8
107130
[isort]: https://pycqa.github.io/isort/
108131
[pipenv]: https://github.com/pypa/pipenv
132+
[pre-commit]: https://pre-commit.com/
109133

110134

111135
### GitHub Actions
@@ -115,7 +139,7 @@ GitHub Actions workflow performs static analysis (`black`, `flake8`, and
115139
`isort`) on committed changes. The workflow is triggered automatically when you
116140
push changes to the main branch or open a pull request.
117141

118-
[workflow-static-analysis]: .github/workflows/python_static_analysis.yml
142+
[workflow-static-analysis]: .github/workflows/static_analysis.yml
119143

120144

121145
## Data sources

0 commit comments

Comments
 (0)