r/learnpython • u/jmichalicek • 4d 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?
2
u/Financial_Morning386 4d ago
I ran into the same thing on Python teams — the annoying part is that
pre-commit installonly 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 runsuv syncand thenpre-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 installand having the hooks run things likeuv run ruff checkrather 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.7
u/Bratmon 4d 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
1
u/ohnoitssobig 4d 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).
3
u/mrswats 4d 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.
1
u/JanEric1 3d ago
You use pre-commit locally and then ALSO run the same checks (+ usually additional slower ones) in CI.
1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 3d ago
At dayjob, we used Lefthook until two days ago when I went and replaced our last instance of it with custom Git hooks. Prior to that, I considered using pre-commit to reduce (or eliminate) our JS/NPM dependencies as Python dependencies are easier to manage security-wise.
However, I realised that we weren't actually making any use of Lefthook's features. We delegated all the linting and formatting to Taskfile tasks (basically Makefiles but way better), so the Git hooks were basically just running
task lint/task format, and by extension it honestly made no sense to even have Lefthook as a dependency at that point.However, even with switching to custom Git hooks, we still needed to tell Git to use them. In our case that was easy, as all I needed to do was plug
git config core.hooksPath .githooksinto the Taskfile task that previously handled installing Lefthook (npm lefthook install). And hey presto, it just works.At dayjob we basically have a separate project that sets up both local and CI dev environments for us, and it handles things like installing Taskfile,
uv, Git, and setting up authentication with the internal company repositories, so we don't need to worry about installing those tools ourselves. However, this would be cumbersome in personal projects (especially if hoping to get other people to contribute), so for those I was somewhat stuck until I found out about Mise.Mise is this handy metatool that handles installing and managing project-specific tooling, meaning every project can define its own set of tools to use (including version pinning and lockfiles), meaning I could set things like Taskfile,
cspell, Markdownlint, anduvto be explicitly required in every project without needing to worry about other people having to manually install those themselves. For example, in my Python-Rust template project I only need to runmise install && task installonce when first installing the project, and from thereon everything just works.I know this isn't a solution fit for everyone or every situation, but it's been working wonders for me lately. I'd probably already use it at work, if I knew how to wire Mise into our private repositories somehow and handle the auth stuff.
Here's an example install script: https://github.com/Diapolo10/python-rust-template/blob/main/tasks/install.yml
1
u/white_nerdy 3d ago
If you want to enforce mandatory linting and formatting policy (or any other machine checkable policy) on a project with multiple devs, you need to set it up in CI. Forcing everybody to install a pre-commit hook is the wrong tool for the job. As you've discovered, inevitably someone somewhere will create a clone that doesn't enforce the policy.
Enforcing the policy centrally and automatically makes the problem go away. You'll waste no more time telling people "Please run the pre-commit hook" because CI will do everything the pre-commit hook does.
1
u/Baner-Fournadet 11h ago
if you already have a makefile or a bootstrap script that sets up the local database and env vars, just tack the hook installation onto that. devs will always run the thing that makes the app actually boot on their machine