Skip to content

Instantly share code, notes, and snippets.

@izikeros
Last active March 14, 2024 10:09
Show Gist options
  • Select an option

  • Save izikeros/937b05e81b5dca81d3daf309ea6bad20 to your computer and use it in GitHub Desktop.

Select an option

Save izikeros/937b05e81b5dca81d3daf309ea6bad20 to your computer and use it in GitHub Desktop.
[pre-commit config] Set of pre-commit hooks for python code quality validation and for code formatting #qa #project #configuration
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
# Use: `pre-commit autoupdate` to update the hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-added-large-files # prevents giant files from being committed.
- id: check-case-conflict # checks for files that would conflict in case-insensitive filesystems.
- id: check-merge-conflict # checks for files that contain merge conflict strings.
- id: trailing-whitespace # trims trailing whitespace.
- id: check-yaml # checks yaml files for parseable syntax.
- id: check-toml # checks toml files for parseable syntax.
- id: end-of-file-fixer # ensures that a file is either empty, or ends with one newline.
- id: mixed-line-ending # replaces or checks mixed line ending.
- id: detect-private-key # detects the presence of private keys.
- id: fix-byte-order-marker # removes utf-8 byte order marker.
- id: check-executables-have-shebangs # ensures that (non-binary) executables have a shebang.
# - id: check-shebang-scripts-are-executable # ensures that (non-binary) files with a shebang are executable.(raises false positives on macOS)
# A tool to automatically upgrade syntax for newer versions of the python language.
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: [ --py311-plus ]
# A formatter for finding and removing unused import statements.
- repo: https://github.com/hadialqattan/pycln
rev: v2.1.2
hooks:
- id: pycln
args: [ --all ]
# Import order and formatting
# If used jointly with flake8-import-order, ensure that flake8 import order
# is set to the same style that is enforced by zimports.
# E.g. add `import-order-style = google` to your .flake8 file.
- repo: https://github.com/sqlalchemyorg/zimports/
rev: v0.6.0
hooks:
- id: zimports
args: [ --style=pycharm ]
# The uncompromising code formatter
- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
- id: black
language_version: python3.11.6
# A tool to automatically fix common style issues in Python code. Has plugins.
- repo: https://github.com/pycqa/flake8
# keep '5.0.4'
rev: '6.1.0'
hooks:
- id: flake8
args: [ --radon-max-cc, "8", --max-function-length, "50" ]
exclude: (__pycache__|.venv|tmp|.tox|experiments)
additional_dependencies: [ flake8-import-order, flake8-force-keyword-arguments, flake8-bugbear, flake8-docstrings, radon, flake8-functions ]
# Find common security issues in your Python code using bandit.
# - repo: https://github.com/Lucas-C/pre-commit-hooks-bandit
# rev: v1.0.6
# hooks:
# - id: python-bandit-vulnerability-check
# args: [ -r, --ini, .bandit ]
# check for docstring coverage
- repo: https://github.com/econchick/interrogate
rev: 1.5.0
hooks:
- id: interrogate
args: [ --quiet, --fail-under=95 ]
# monitor code’s complexity
- repo: https://github.com/rubik/xenon
rev: v0.9.0
hooks:
- id: xenon
args: [ --max-absolute B, --max-modules A, --max-average A ]

pre-commit hooks

Set of pre-commit hooks for python code quality validation and for code formatting.

code formatting

  • black - "The Uncompromising Code Formatter"
  • zimports (import ordering) - needs configuration (select style for zimports and flake8-import-order plugin)
  • trailing-white-spaces - remove white spaces at the end of line
  • end-of-file-fixer - enforce empty line at the end of file
  • mixed-line-ending - normalize line endings

linter

  • flake8 - multi purpose linter with plugins, used with .flake configuration file (see: .flake8 gist), added flake8-import-order

code

  • pyupgrade - automatically upgrade syntax for newer versions of the language (enforce modern python style/syntax)

security

  • bandit - requires configuration in .bandit file

Other

For more ideas of hooks see: pre-commit hooks

interesting hooks to look into:

  - repo: https://github.com/pre-commit/mirrors-prettier
    rev: v2.5.1
    hooks:
      - id: prettier
        files: \.(js|ts|jsx|tsx|css|less|html|json|markdown|md|yaml|yml)$

Credits: I found multiple interesing hooks on page: https://interrupt.memfault.com/blog/pre-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment