diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..be2fd60e --- /dev/null +++ b/.flake8 @@ -0,0 +1,10 @@ +[flake8] +max-line-length = 119 +select = + F541, # f-string without any placeholders + F841, # local variable 'x' is assigned to but never used + F401, # 'x' imported but unused + E741, # ambiguous variable name 'l' + F821, # undefined name 'x' + E266, # too many leading '#' for block comment + diff --git a/.github/workflows/code-linting.yml b/.github/workflows/code-linting.yml index 0a56d2e8..0448e1d9 100644 --- a/.github/workflows/code-linting.yml +++ b/.github/workflows/code-linting.yml @@ -19,17 +19,90 @@ on: workflow_call: jobs: - lint-check: - name: Lint check + linting: + name: "Code Linting" runs-on: ubuntu-latest steps: - - name: Checkout repository + - name: Checkout uses: actions/checkout@v4 + + - name: Get changed files + id: changed-files + uses: step-security/changed-files@v45.0.1 with: - submodules: 'recursive' + files: | + **/*.py + + - name: Run PyLint + id: pylint + env: + CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + SKIP_DOCS: ${{ contains(github.event.pull_request.labels.*.name, 'skip-docs') }} + SKIP_LINTING: ${{ contains(github.event.pull_request.labels.*.name, 'skip-linting') }} + run: | + if [[ -z "$CHANGED_FILES" ]]; then + echo "Nothing to lint." + echo "exit-code=0" | tee -a "$GITHUB_OUTPUT" + exit 0 + fi + + if [[ $SKIP_DOCS == true ]]; then + ADDITIONAL_PYLINT_ARGS="--disable=C0115,C0116" + else + ADDITIONAL_PYLINT_ARGS="" + fi + + if [[ $SKIP_LINTING == true ]]; then + ADDITIONAL_PYLINT_ARGS="--exit-zero" + fi + + pip install pylint + set +e + pylint $ADDITIONAL_PYLINT_ARGS --output pylintrc.txt --rcfile .pylintrc ${CHANGED_FILES[@]} + echo "exit-code=$?" | tee -a "$GITHUB_OUTPUT" - - name: Check lint + - name: Run flake8 + id: flake8 + env: + CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + SKIP_LINTING: ${{ contains(github.event.pull_request.labels.*.name, 'skip-linting') }} run: | - pip install pre-commit==3.6.0 - pre-commit install - pre-commit run --all-files --show-diff-on-failure --color=always + if [[ -z "$CHANGED_FILES" ]]; then + echo "Nothing to lint." + echo "exit-code=0" | tee -a "$GITHUB_OUTPUT" + exit 0 + fi + + if [[ $SKIP_LINTING == true ]]; then + ADDITIONAL_FLAKE8_ARGS="--exit-zero" + else + ADDITIONAL_FLAKE8_ARGS="" + fi + + pip install flake8 + set +e + flake8 $ADDITIONAL_FLAKE8_ARGS --output flake8.txt --config .flake8 ${CHANGED_FILES[@]} + echo "exit-code=$?" | tee -a "$GITHUB_OUTPUT" + + - name: Summary + env: + PYLINT: ${{ steps.pylint.outputs.exit-code == 0 }} + FLAKE8: ${{ steps.flake8.outputs.exit-code == 0 }} + run: | + if [[ "$PYLINT" != "true" ]]; then + echo "Pylint output:" | tee -a $GITHUB_STEP_SUMMARY + echo '```' | tee -a $GITHUB_STEP_SUMMARY + cat pylintrc.txt | tee -a $GITHUB_STEP_SUMMARY + echo '```' | tee -a $GITHUB_STEP_SUMMARY + fi + + if [[ "$FLAKE8" != "true" ]]; then + echo "Flake8 output:" | tee -a $GITHUB_STEP_SUMMARY + echo '```' | tee -a $GITHUB_STEP_SUMMARY + cat flake8.txt | tee -a $GITHUB_STEP_SUMMARY + echo '```' | tee -a $GITHUB_STEP_SUMMARY + fi + + if [[ "$PYLINT" != "true" || "$FLAKE8" != "true" ]]; then + exit 1 + fi diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 00000000..6debf011 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,6 @@ +[MAIN] +ignore-paths=tests +max-line-length=119 +ignore-patterns=.*\.ipynb$ +recursive=y +