Skip to content

Commit

Permalink
STYLE: Format Python code
Browse files Browse the repository at this point in the history
Format Python code.

Add a GHA workflow using pre-commit to make sure that the Python code is
formatted appropriately according to the specified config file.

Documentation:
https://pre-commit.com/hooks.html
  • Loading branch information
jhlegarreta committed Oct 22, 2023
1 parent 2ba47ea commit b8168f1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/check_format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: code format

on: [push, pull_request]

jobs:
pre-commit:

runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python-version: ['3.10']
requires: ['latest']

steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install and run pre-commit hooks
uses: pre-commit/action@v3.0.0
50 changes: 50 additions & 0 deletions pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
default_language_version:
python: python3.10

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: check-added-large-files
name: check-added-large-files
description: Prevent giant files from being committed.
- id: check-ast
name: check-ast
description: Simply check whether files parse as valid python.
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-merge-conflict
name: check-merge-conflict
description: Check for files that contain merge conflict strings.
- id: check-toml
- id: check-yaml
exclude: .github/workflows
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/timothycrosley/isort
rev: 5.12.0
hooks:
- id: isort
name: isort
args: [--settings-path, ./pyproject.toml]
types: [python]

- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
- id: black
name: black
args: [--config, ./pyproject.toml]
types: [python]

- repo: https://github.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
name: flake8
additional_dependencies: [flake8-docstrings==1.6.0]
args: [--config, ./setup.cfg]
types: [python]
26 changes: 26 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[tool.black]
line-length = 79
target-version = ["py310"]
exclude ='''
(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
| data # also separately exclude project-specific files
# and folders in the root of the project
)
'''

[tool.isort]
profile = "black"
line_length = 79

0 comments on commit b8168f1

Please sign in to comment.