Skip to content

Commit

Permalink
ci: set up GH action that enforces black formatting on changed lines (#…
Browse files Browse the repository at this point in the history
…129)

This is achieved via the Python package `darker`, which checks that all
changed lines are formatted with "black" style. The intent is to strike
a balance between enforcing a style but not doing one large commit that
only reformats everything, which messes up git history. After this CI
rule has run for some time, the (hopefully small) remaining part of the
code can still be reformatted with black in one commit and black be
enforced from that moment of time.

I have done a quick comparison of various formatters and black seems to
be the one that results in a formatting that is closest to the current
format, so enforcing that in CI seems like the best choice.

Signed-off-by: Ingo Müller <[email protected]>
  • Loading branch information
ingomueller-net authored Nov 8, 2024
1 parent 4cc3091 commit fda68c9
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/python_formatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Python Formatting

on: [push, pull_request]

jobs:
darker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
cache: 'pip'
- name: Install darker
run: pip install darker
- name: Run darker
run: |
darker --revision origin/main . --diff --check || (echo "Formatting of changed lines does not respect 'black'. Please run 'darker --revision origin/main .' and commit the changes."; false)

0 comments on commit fda68c9

Please sign in to comment.