Linting

Using a lint-tool to perform static source code analysis and checking for syntax discrepancies. They clean the code by standardizing it, prevent syntax errors and bad formatting, save developers time, and create better code with meaningful change for code review process.

Some lint tools for python include: Flake8, black, isort

It’s standard to use linting with git commit hooks such as pre-commit hook.

Pre-commit Framework

pre-commit is a framework for multi-language pre-commit hooks, installed as a python package.

  1. Installation:
pip install pre-commit
  1. create configuration file by adding file named .pre-commit-config.yaml to project directory.
    Running pre-commit sample-config command(documentation) can give you sample content for this file. to see list of possible configurations visit plugins page.
    Sample .pre-commit-config.yaml file:
exclude: ^\. # Exclude every file or folder starting with a dot.
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.6.0
    hooks:
    -   id: trailing-whitespace
        exclude: ^datasets/
        types: [text]
    -   id: end-of-file-fixer
        types: [text]
    -   id: check-added-large-files
    -   id: check-case-conflict
    -   id: check-yaml

-   repo: https://github.com/pycqa/isort
    rev: 5.13.2
    hooks:
    -   id: isort
        name: isort
        args:
        -   --filter-files
        -   --profile=black

-   repo: https://github.com/psf/black
    rev: 24.4.0
    hooks:
    -   id: black
        files: ^project/
        types: [python]
        require_serial: true
    -   id: black-jupyter
        files: ^notebooks/
        types_or: [python, pyi, jupyter]
        require_serial: true
        additional_dependencies: [".[jupyter]"]

-   repo: https://github.com/pycqa/flake8
    rev: 7.0.0
    hooks:
    -   id: flake8
        exclude: migrations/.*|^project/project/settings/
        additional_dependencies:
        -   flake8-black
        args: [
          '--max-line-length=256',
          '--ignore=E203,E402,E501,E800,W503',
          '--select=B,C,E,F,W,T4,B9'
        ]
Note

In case of issues with opening older pre-commit configurations, attempt updating packages to latest version using following command:

pre-commit autoupdate

this will update pre-commit confit file, and install latest packages used by pre-commit.

  1. Install the git hook scripts:
pre-commit install
  1. Run against all the files
pre-commit run --all-files --show-diff-on-failure