Keep your code tidy with simple pre-commit hooks

- 1 min

Objective

Simple code quality checks allow Data Scientists to mantain good coding practice across the projects. Below are some pre-commit hooks which enable these checks.

Pre-requisities

Make sure you have pre-commit installed in your environment. Note pre-commit requires python-3.6+.

$ pip install pre-commit

Setup

Create .pre-commit-config.yml file in the root of your project and add the following configuration to it:

repos:
-   repo: https://github.com/ambv/black
    rev: stable
    hooks:
    - id: black
      language_version: python3.10 # use your python version here
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.3.0
    hooks:
    - id: flake8
    - id: check-yaml
    - id: end-of-file-fixer
    - id: trailing-whitespace
    - id: requirements-txt-fixer
-   repo: https://github.com/asottile/reorder_python_imports
    rev: v2.1.0
    hooks:
    - id: reorder-python-imports
-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.761
    hooks:
    - id: mypy

How to run it

Once you have the pre-commit framework installed and your .pre-commit-config.yml file is ready, run pre-commit install at the source directory to set up the hooks specified in your configuration file.

Post installation, pre-commit will run automatically on git commit. Please rectify all code quality issues reported by flake8. Others will auto-correct your code.

Viktoriia Oliinyk

Viktoriia Oliinyk

Building Responsible AI

comments powered by Disqus