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


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

  - repo:
    rev: 22.3.0
      - id: black
        args: [--diff, --check]

  - repo:
    rev: v3.0.0a3
      - id: pylint
        args: [--disable=all, --enable=unused-import]

  - repo:
    rev: v0.902
      - 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?