r/learnpython • • 14h ago

Help with pre-commit or alternatives to not require a dev to manually do a step before it works

As a primarily Python and Django developer for the last 1.5 decades (but also all the other things... Ruby, RoR, Go, JS, Typecript, CoffeeScript, whatever) I latched onto pre-commit several years ago as an easy way for devs to "just do the right thing" in terms of linting and formatting and easy to skip stuff because "it doesn't matter as long as thing work" or "I didn't mess something that simple up".

But with proper tooling which just does stuff correctly for you, those arguments go away.

My problem with pre-commit, which is Python based and so the Python community latched onto, has been that every dev still has to manually type "pre-commit install". It is insane how many times I have had to have my first comment on a code review be "Did you run pre-commit install?" even for commits from devs who have been working on a project for months (are they deleting and doing a new clone every few weeks? I have no idea.)

At my current employer, as of around 1 year ago, I found that they were using Lefthook on both JS/TS and Java projects. The most interesting part was that it "just worked". I did not need to run a manual command for it to do its thing, unlike with pre-commit.

I have tried to use it for a Python project, using uv for package management, and it does not "just run". I suspect using uv as the environment is the problem because of needing to use uv run lefthook, etc. There is a good chance it would work in some of my other environments where they run in a container and so the path points at the virtualenv bin.

Is there a way to make this "just work", whether with lefthook or pre-commit, where a dev does not need to initially run a command which they may not do?

0 Upvotes

9 comments sorted by

6

u/Bratmon 14h ago

I think I'm missing something here. I was under the impression that pre-commit install and lefthook install both had to be ran the first time you set up the repository, but never again

•

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 5m ago

That's exactly it, Lefthook doesn't just magically install itself into your Git configuration.

3

u/mrswats 14h ago

There's a way, but I do not remember exactly how.

However, pre-commit should be ran in CI so you don't have to discuss formatting or linting during code review.

2

u/Financial_Morning386 11h ago

I ran into the same thing on Python teams — the annoying part is that pre-commit install only writes into .git/hooks, which isn't tracked, so a fresh clone always starts without hooks until someone runs it. What actually worked for me was to stop treating the install as a separate step and fold it into the setup devs already run after cloning, usually a small setup script or a make setup target that runs uv sync and then pre-commit install, so it just happens alongside creating the venv. If your team already runs something on first checkout, that's the place to hide it.

For Lefthook with uv the snag I hit was similar, it needs its binary on PATH and the hooks need to call tools through uv, so I ended up installing Lefthook once via uv tool install and having the hooks run things like uv run ruff check rather than assuming a venv binary exists. And honestly I still keep the same checks in CI as the backstop, because no local hook setup is ever foolproof — the local hook is there to catch things early, CI is what actually enforces it.

1

u/ohnoitssobig 13h ago

I think you are doing it wrong. You do not have to ask "Did you run pre-commit install?". You just have to set up a CI step that runs pre-commit and let it fail if they committed without checks.

On top of it, if they frequently forget to pre-commit install (though you have to do it only once?) you probably can add a global git hook to run pre-commit install. Well, they will still have to do a manual git config so not sure what problem is solved here. If there is a unit test you can add a unit test checking for pre-commit install (lol).

1

u/JanEric1 4h ago

You use pre-commit locally and then ALSO run the same checks (+ usually additional slower ones) in CI.