Skip to content

Instantly share code, notes, and snippets.

@hugodahl
Last active February 13, 2021 22:06
Show Gist options
  • Save hugodahl/82c8ef5a8ef3b68229b0043c150cc7f5 to your computer and use it in GitHub Desktop.
Save hugodahl/82c8ef5a8ef3b68229b0043c150cc7f5 to your computer and use it in GitHub Desktop.
Calling pylint from the pre-commit hook
repos:
- repo: https://github.com/python/black
rev: stable
hooks:
- id: black
- repo: https://github.com/fsfe/reuse-tool
rev: latest
hooks:
- id: reuse
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: local
hooks:
- id: "pylint-library"
name: "pylint-library"
entry: /usr/bin/env bash -c
args: ['pylint $( find . -path "./adafruit*.py" )']
language: system
description: Run pylint rules on library .py files
- id: pylint_examples
name: pylint-examples
entry: /usr/bin/env bash -c
args: ['([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace $( find . -path "./examples/*.py" ))']
language: system
description: Run example-specific pylint rules

Purpose

Calling pylint from the pre-commit hooks, rather than individual GitHub workflow steps. Allows it to be called on a contributor's computer before being pushed to GitHub and failing a test build

Changes

The changes are relatively simple. It only requires adding two (2) new hooks against a placeholder local repo. The two separate calls in the included file have to do with the different calls and parameters currently in the workflow. The separate calls are as follows, and match the current process within the .github/workflows/build.yml configuration:

  1. The pylint execution with the configuration values within the .pylintrc file, without exception. This is for all files and/or directories (and their contents) which start with adafruit_ and end in .py. Following the default naming patterns, particularly when using Cookiecutter and the Adafruit Cookiecutter template, should catch and lint all library-only files, and not the examples or other files.

  2. The pylint execution with with the configuration from the .pylingrc file, but with some rules or checkes disabled for the example scripts. That means that linting is only performed on *.py files within the /examples/ directory, if there is such a directory. The difference with the library rules are that the following rules/checks are disabled:

  • missing-docstring
  • invalid-name
  • bad-whitespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment