What hook language should I use for pre-commit hooks written in Python?

Question:

I’m working on a local pre-commit hook written in Python. At first, I saw this quote from the pre-commit documentation:

python

The hook repository must be installable via pip install . (usually by either setup.py or pyproject.toml).

I made the repo with the local hook installable using pip install ., but later found out that that isn’t supposed to work with local hooks.

More recently, I created a bare-bones .pre-commit-config.yaml that looks like this:

repos:
    -
        repo: local
        hooks:
            -
                id: &id1 test1
                name: *id1
                language: python
                entry: python -c "print('Hello, world!')"
                always_run: true
            -
                id: &id2 test2
                name: *id2
                language: system
                entry: python -c "print('Hello, world!')"
                always_run: true

Both seem to do the same thing.

Here’s my question: for local hooks, is there any difference between using the python language and the system language? Should I prefer one over the other when writing hooks in Python? I would think that system hooks would rely on the user’s PATH (and that python hooks might not), but the python command isn’t on my PATH normally, and the hook still works (I start a nix-shell when I want to run python).

Asked By: Ginger Jesus

||

Answers:

repo: local hooks do not "install" anything beyond additional_dependencies: https://pre-commit.com/#repository-local-hooks

a language: system hook cannot install anything — it is the escape hatch for unsupported things where it is on the user to make sure their environment is provisioned correctly

a language: python hook will set up a virtualenv and install things. in the case of repo: local this would just be additional_dependencies

~generally language: python is more portable (the point of pre-commit is it installs and manages the tools so your contributors don’t need a big complicated setup script). and even without dependencies it’s slightly more portable because python will be available for use in entry on all the platforms via virtualenv


disclaimer: I wrote pre-commit

Answered By: anthony sottile
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.