How to use Black, Pylint and Mypy in Pre-commit?

python

Python’s not the strictest language, so to have any confidence in your code you need to hit it with a barrage of checks to ensure it meets at least some level of quality.

I use the following code quality checks:

The following Pre-commit hooks will check your code when you try to commit, catching problems before they reach your repository.

How to install the Pre-commit hooks

Add the following .pre-commit-config.yaml file to the root of your repository.

fail_fast: true

repos:
  - repo: https://github.com/ambv/black
    rev: 22.3.0
    hooks:
      - id: black
        args: [--diff, --check]

  - repo: https://github.com/pre-commit/mirrors-pylint
    rev: v3.0.0a3
    hooks:
      - id: pylint
        args: [--disable=all, --enable=unused-import]

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.902
    hooks:
      - id: mypy
        exclude: ^tests/
        args: [--strict]

Install Pre-commit and add the git hooks:

pip install precommit
pre-commit install

Notes on Black

Recommended: How to use Black, Pylint and Mypy in Github Actions?