Skip to content

Commit

Permalink
Utility to format Python code before pushing new commits
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Sep 9, 2024
1 parent 4245137 commit 8b53e78
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions format-python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

BASE_REVISION=$1

echo $BASE_REVISION


if [ -z "$BASE_REVISION" ]; then
BASE_REVISION="beta"
elif [ "$BASE_REVISION" == "-l" ] || [ "$BASE_REVISION" == "--last" ]; then
BASE_REVISION=$(git log --oneline| head -n 1 | awk '{ print $1}')
fi

# First, do not touch formatting but fix:
# - single/double quotes (--select Q)
# - sort imports (--select I)
# - unused imports (--select F401)
# - deprecated `mock` (--select UP026)
# - extraneous-parentheses (--select UP034)
# - Unnecessary parentheses after class definition (--select UP039)
# - Indentation warning (--select W1)
# - No newline at end of file (--select W292)
echo "Using ruff..."
git diff --name-only "$BASE_REVISION" | grep '.py' | xargs ruff check --select Q --select I --select F401 --select UP026 --select UP034 --select UP039 --select W292 --fix

# Applying Black format on changes (since $BASE_REVISION)
echo "Using darker..."
darker -i -r "$BASE_REVISION"

0 comments on commit 8b53e78

Please sign in to comment.