Skip to content

Use pre-commit CI to automatically fix code style issues #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions .github/workflows/python_static_analysis.yml

This file was deleted.

35 changes: 35 additions & 0 deletions .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Static Analysis

on:
pull_request:
push:
branches: [main]
workflow_dispatch:

jobs:
job:
runs-on: ubuntu-latest

steps:

# https://github.com/actions/setup-python
- name: Install Python 3.11
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install Python dependencies
run: |
pip install --upgrade pip
pip install black 'black[jupyter]' flake8 isort
pip install pre-commit

# https://github.com/actions/checkout
- name: Checkout quantifying
uses: actions/checkout@v4
with:
path: quantifying

- name: pre-commit
run: pre-commit run --show-diff-on-failure --color=always --all-files
working-directory: ./quantifying
57 changes: 57 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
default_language_version:
python: python3.11

ci:
autofix_commit_msg: |
[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
autofix_prs: true
autoupdate_branch: ''
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
autoupdate_schedule: weekly
skip: []
submodules: false

repos:

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
- id: destroyed-symlinks
#- id: end-of-file-fixer
#- id: mixed-line-ending
# args:
# - --fix=no
#- id: trailing-whitespace

- repo: local
hooks:
- id: black
name: "Black"
entry: black
language: system
types: ["file", "python", "text"]

- id: flake8
name: "Flake8"
entry: flake8
language: system
types: ["file", "python", "text"]

- id: isort
name: "isort"
entry: isort --filter-files
language: system
types: ["file", "python", "text"]
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ follow these steps:

### Static analysis

#### Using [`pre-commit`][pre-commit]
1. Install pre-commit

- Using pip:
```shell
pip install pre-commit
```
- Using homebrew:
```shell
brew install pre-commit
```

2. Install the git hook scripts
```shell
pre-commit install
```

It will run on every commit automatically.

#### Using [`dev/tools.sh`][tools-sh] helper script
The [`dev/tools.sh`][tools-sh] helper script runs the static analysis tools
(`black`, `flake8`, and `isort`):
```shell
Expand All @@ -89,6 +109,7 @@ directories to check:
```

[tools-sh]: /dev/tools.sh
[pre-commit]: https://pre-commit.com/


### Resources
Expand All @@ -100,12 +121,15 @@ directories to check:
- [isort][isort]: _A Python utility / library to sort imports_
- (It doesn't import any libraries, it only sorts and formats them.)
- [ppypa/pipenv][pipenv]: _Python Development Workflow for Humans._
- [pre-commit][pre-commit]: _A framework for managing and maintaining
multi-language pre-commit hooks._

[ccospyguide]: https://opensource.creativecommons.org/contributing-code/python-guidelines/
[black]: https://github.com/psf/black
[flake8]: https://gitlab.com/pycqa/flake8
[isort]: https://pycqa.github.io/isort/
[pipenv]: https://github.com/pypa/pipenv
[pre-commit]: https://pre-commit.com/


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

[workflow-static-analysis]: .github/workflows/python_static_analysis.yml
[workflow-static-analysis]: .github/workflows/static_analysis.yml


## Data sources
Expand Down