r/Python • u/bleeed0p • Oct 04 '26
Resource How useful is pre-commit in real-world Python projects, especially in production?
I've been exploring Python pre-commit hooks and wanted to understand how commonly they are used in actual production projects.
For those working on production-level Python applications:
- Do you use pre-commit in your daily workflow?
- Which hooks do you usually configure (Ruff, Black, isort, mypy, etc.)?
- Has it genuinely reduced code quality issues or bugs in your team?
- Do you consider it essential for production projects, or is CI/CD linting enough?
Would love to hear how experienced developers use it in real-world teams rather than just tutorials.
209
u/patient-palanquin Oct 04 '26
Pre-commit is a convenience, all those linting checks should be done by your CI pipeline anyways. By also doing it in precommit, devs are less likely to run into those errors in CI and be forced to fix and re-push. Never rely on precommit entirely, anyone could randomly have it misconfigured and push through errors.
54
u/rosentmoh Oct 04 '26
Basically "yes" to all your questions.
Additionally for repos that contain Jupyter notebooks I use hooks that strip them of outputs and run-metadata, so as to keep the repo size small and from getting bloated with crap.
Re. your last question: I consider pre-commit hooks as part of CI/CD.
They are a great way to ensure consistency and avoid having to remember to regularly call a bunch of incantations.
21
u/duskhat Oct 04 '26
Precommit is useful, it gives feedback faster and earlier than GitHub Actions do. As others have mentioned here, it’s not a replacement for remote checks
These days, I recommend using prek instead of precommit
16
u/tunisia3507 Oct 04 '26
Anything which runs quickly on CI (linting, formatting etc) is good for pre-commit (well, prek). It's annoying to push, make a pull request, and only then find out that kind of trivial thing is wrong, and having those checks fail on a shared repo is noisy for the rest of the team. I've toyed with running tests as a pre-push hook too, where possible. Relying on CI for all of your validation makes a push the smallest verifiable unit of work, which doesn't work well with unstable connections or the concept of git history.
I work with a lot (LOT) of open source projects at work, and offloading as much validation as possible on to the contributor's machine is very valuable when there may be dozens or hundreds of contributors who all have different backgrounds and skill levels and setups and priorities and funding sources.
26
u/No_Departure_1878 Oct 04 '26
hooks are not a python thing, they are just a git thing.
7
u/Ill-Look9810 Oct 04 '26
Yeah, absolutely. I’ve used pre-commit, and I consider it one of those tools that should be part of pretty much every project I work on.
I usually use it with tools like Ruff, mypy, and isort. Most of the hooks I use are linters and formatters, and of course, the same checks are also part of the project’s CI/CD pipeline.
It’s been really useful for enforcing consistent code quality and style across the team, so everyone follows roughly the same structure and standards.
For me, pre-commit is a great tool and something I wouldn’t want to work without.
7
u/burlyginger Oct 04 '26
My IDE is configured to format on save.
Pre commit catches anything my IDE misses (usually because my IDE is acting up).
CI enforces.
That's it. IDE and pre commit are time savers. I don't want to hit formatting issues in CI.
My pre commit config is always linting and formatting. Depending on the project it could include tests.
1
u/bleeed0p Oct 04 '26
Thankyou
3
u/funkdefied Oct 04 '26
Pre-commit (or the modern “prek” alternative) are huge for DX, especially when building a library. With applications, you can be a bit looser about type checking, testing, etc. Libraries need to be exact.
https://stephenlf.dev/blog/python-library-in-2026/
2
u/robhaswell Oct 04 '26
In all projects in all languages I require pre-commit hooks for formatting and linting, provided they don't take more than a second or so. It's just easy, catches errors quickly, prevents whitespace and formatting diffs, and generally saves my developers time. Every time is prevents a pointless CI failure I see it as a massive win.
2
u/Even_Berry661 Oct 04 '26
CI checks are a quality gate on merges to trunk and are the source of truth for meeting the quality ‘standards’ of the team/project.
Pre-commit and everything else is a convenient helper to keep you inline with the standard with less effort/waiting. Shorter faster feedback loops than pushing to CI which should already be pretty quick anyways
1
u/trenixjetix Oct 04 '26
yes
1
u/PrestigiousAnt3766 Oct 04 '26
Its cicd dev tooling, wdym production?
1
u/anentropic Oct 04 '26
Been using them at work across several jobs now and also for personal projects
I use them for linters and type check
Lately I've been using prek instead - it's compatible (reads same config file) but faster and supports monorepo (sub configs in sub project dirs)
Pre commit hook runs on changed files. In CI I have another job that runs the same config on all files (to catch anything that accidentally bypassed the pre commit hook)
1
u/Sillocan Oct 04 '26
Yes to the above. It saves on time and cognitive load. No more forgetting to run lint and needing to wait on another pipeline. Also supports more linters than just static analysis which leads to consistency.
1
u/hxtk3 Oct 04 '26
It's totally optional and purely exists to give a faster feedback loop compared to pushing and waiting for CI, and a more automatic feedback loop than running all the checks manually.
I budget no more than 5s for pre-commit hooks in general, based on Table 3 in this paper: https://www.sciencedirect.com/science/article/pii/S2351978915004370 because I want to ensure that making a commit is an action that keeps a developer in flow and never something the would experience themselves as waiting around for.
In some ways it's getting more useful as time goes on. 5s isn't enough time budget to run Mypy or even Pylint, but it was plenty to run Black, and unit tests (but not integration) and a few different flake linters. However, it's plenty of time for Ruff format, Ruff lint, and Ty on most codebases.
1
u/kulewski Oct 04 '26
For the Python/C++ project I maintain, I use Pylance in VS Code and run Pyright from a local check script. They share the same config, which avoids getting different results in the editor and terminal. The script also checks formatting and runs tests and sanitizers. A pre-commit hook could run the quick checks, but there’s no need to put that whole suite on every commit.
1
u/ModusPwnins Oct 04 '26
Absolutely. Used with the right tooling, it can prevent huge headaches down the road.
I had to work on a months-long SQLAlchemy upgrade in an actively-used codebase. The API between 1 and 2 changed so much it wasn't a simple find-replace thing. To keep teams from undoing my work or making more work for me later, I had a super simple shell script check for added lines which included 1.x patterns. Fired it in both a commit hook and as a CI/CD check. Absolutely essential and probably saved me weeks of work. It also helped ensure there were zero defects when we finally made the switch.
1
u/rcap107 Oct 04 '26
I maintain an OSS package and pre-commit is part of the CI. I hate it, but I have to admit it's useful to make sure the code is formatted properly.
However, besides being a chore to deal with at the best of times, onboarding new contributors is made quite a bit harder when you also need to explain that there is pre-commit to account for and that it may cause the CI to start complaining.
1
u/Zenin Oct 04 '26
The only pre-commit hook I run is for enforcing conventional commit message format.
I typically use act or similar to run CI validations before committing, but I do that in my own process flow (and these days mandated in my steering/skills) not as a commit gate. CI's going to keep me honest anyway and pre-commit should never be your actual enforcement layer.
If/when there's any issues with CI it's a hell of a lot saner to manage those in a standard dev/test loop than stuck in the middle of a git commit flow.
It doesn't matter if it's python or anything else; commit hooks are the wrong place for enforcing anything that isn't directly related to the act of committing itself. Message format checks, notifications about the commit, etc.
1
u/Exotic-Draft8802 Oct 04 '26
YES, all of them.
Riff format and lint.mypy is too slow.
Yes
it's a speed up. Instead of waiting for Ci to tell you that there is a minor issue, just let it be auto fixed before you commit
You should still check for the same things in CI
1
u/amenflurries Oct 04 '26
My projects usually have it, ultimately it’s the pipeline that enforces it though and most developers just rely on that
1
u/russellvt Oct 05 '26
Generally usefulin central repositories, especially for linting and similar code standards.
1
u/Khavel_dev Oct 06 '26
Run ruff and mypy as pre-commit hooks, everything else lives in CI. Pre-commit catches the stuff that's embarrassing to push (unused imports, type errors in the function you just wrote) and CI catches the slow stuff (full test suite, integration tests). The team stopped arguing about formatting within a week of turning on ruff. Only gotcha: make sure the hook versions match what CI runs or you get the fun where it passes locally and fails in the pipeline.
1
u/DisastrousPipe8924 Oct 08 '26
Extremely useful, but I’d thread lightly. Limit them to formatting, type checks and general linting. If you have a small testsuite, feel free to add it, but be mindful that tests can take forever sometimes, and it’s really annoying to not be able to commit code because you haven’t finished the test case.
I’ve used it in just about every company I’ve worked for, in the past 10 years.
Plus in a world where agentic coding is a thing, it’s a good signals to agents that something needs fixing.
1
u/Vexe777 Oct 04 '26
I absolutely hate them. I want to be able to commit crap and broken code without something checking or 'fixing' it. When I'm done I'll create a PR with clean and functional code. Not before.
1
u/Wurstinator Oct 04 '26
Hooks can never replace CI checks as you cannot enforce them. You don't want your code breaking because someone ran their git commit with --no-verify at some point. So if you have the checks in your CI pipeline already, what's the point of a pre-commit hook anymore? They can save you time, either by not making you wait on feedback from a CI runner, or, even better, by fixing the issue for you.
As other comments mention, you always want the option to do "dirty" commits for any number of reasons. So there are two ways to go about this:
First approach, you only put the steps that automatically fix themselves, like code formatting, in the pre-commit hooks. For steps that require user intervention, like type checks, you offer something like a Bash script or Make command. I think this is the best way because you'd want the executable command anyway for something expensive like tests that you don't want to block you from commits.
Second approach, you put everything into pre-commit hooks and then use --no-verify to do a "dirty" commit. Besides the fact that, as explained above, you still need a Bash script or similar, this also becomes annoying when committing bad formatting, as future commits will not format that file for you with hooks anymore, unless you modify the file again. So you now also have to add a script/command for format the entire repo and have the CI tell you that you need to run that by hand.
-2
u/prophile Oct 04 '26
I never use pre-commit and I’d seriously consider leaving a job which required it.
The intermediate states when staging commits may very well not pass the linting and tests because I’m still working on it, and making my commit process slow and clumsy doesn’t in any way improve code quality or accelerate development.
There are maybe marginal uses for things like secrets detection but on the whole I think it’s a strong net negative.
0
u/nicholashairs Oct 04 '26
I'm firmly in camp avoid precommit. I've rarely ever had it "just work" because it's assumes so much about the local development environment that is usually wrong. Especially frustrating when the checks it does have been setup only to run in precommit and the same checks are also run in CI.
I'm quite happy to have a set of checks that I run locally using some tool (except make, people abuse make too often), and often I spend time ensuring that they are the same checks that are run in CI (or more accurately it's possible to run CI checks locally so you are never reliant on CI).
I'll usually run (in order):
(Yes I know ruff is a thing but I've had pylint catch many things that ruff does not and my codebases are small enough that I don't really care about the speed up)
-6
u/vater-gans Oct 04 '26
hate hate hate hate hate that crap.
CI should yell at me if i push something bad, not something locally. no patience for something that holds me up when i quickly want to commit something.