r/learnpython • • 1h 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

3 comments sorted by

4

u/Bratmon 1h 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

2

u/mrswats 1h 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.

•

u/ohnoitssobig 30m 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).