Francisco Camargo

Logo

Data Scientist & Machine Learning Engineer

View the Project on GitHub francisco-camargo/francisco-camargo

pre-commit

Return to top README.md

Installation

Docs

pip install pre-commit

validate with

pre-commit --version

Usage

Create a .pre-commit-config.yaml file underneath the parent directory. Populate this file with all the pre-commit hooks of interest. Can get a sample set of hooks by running

pre-commit sample-config

then copying and pasting the returned text into .pre-commit-config.yaml

Supported hooks

Next, in order to add the desired hooks into the .git hook structure, run

pre-commit install

At this point, whenever you run git commit the hooks will be run first. If you would like to run the hooks on their own, run

pre-commit run --all-files

This can be useful if you want to run the hooks without actually making a new git commit

If you want to make a commit without running pre-commit, use the --no-verify option, e.g.

git commit --no-verify

If in the future you add more hooks to .pre-commit-config.yaml, you don’t have to re-run pre-commit install.

Guide to exclude specific folders from being checked during run of pre-commit

pre-commit autoupdate

The versions of each repo used for a hook must be stated explicitly within .pre-commit-config.yaml which implies needing to manually update these values over time. However, you can update these values automatically by running

pre-commit autoupdate

Setting up detect-secrets

The detect-secrets hook requires a baseline file to work properly. This file contains a record of all current “secrets” in your codebase that have been reviewed and approved.

Initial Setup

Before running pre-commit for the first time with detect-secrets, create the baseline file:

detect-secrets scan --baseline .secrets.baseline

This will scan your entire repository and create a .secrets.baseline file containing any detected potential secrets.

Review and Update

After creating the baseline, review the detected items:

detect-secrets audit .secrets.baseline

This opens an interactive audit where you can mark each detected item as a real secret (to be excluded) or a false positive (to be ignored in future scans).

Adding to Git

The .secrets.baseline file should be committed to your repository:

git add .secrets.baseline
git commit -m "Add secrets baseline for detect-secrets"

Updating the Baseline

When you add new legitimate “secrets” (like test API keys, example configurations, etc.), update the baseline:

detect-secrets scan --baseline .secrets.baseline --update

Then audit the new findings and commit the updated baseline file.

mypy

Cheat sheet for mypy

pre-commit PyTest

It is possible, though not recommended, to run PyTest as a pre-commit hook. If you want to do it anyway, here are some example configurations.

Suggested Settings

link

The .pre-commit-config.yaml I use lives in repo-template

Code Format (old)

guide for using black and flake8 and isort in a pyproject.toml, also talks about using a .pre-commit-config.yaml file.

black guide

Code formatting shortcut in VSCode Alt+Shift+f or look for Format Document in the command palette

Gonna go with black, guide. Add it to requirements.txt

Run black by running

black <source_file_or_directory>

This worked right away. However I’m not a fan of using double-quotes instead of single quotes, so one option is to pass --skip-string-normalization in the command line or to insert it into the settings, guide. -S for short

flake8 guide

Run flake8 by running

flake8

Configure how flake8 runs by having a setup.cfg file, for example,

[flake8]
exclude =
    .git,
    __pycache__,
    env,
max-line-length = 88

This prevents flake8 from looking at files we are not interested in adding to a code repo. This also changes the maximum line length to align with black

docstrings

guide

guide to popular docstring formats