Skip to content

replace pre-commit with prek#402

Merged
Theoreticallyhugo merged 2 commits into
mainfrom
feat/switch-to-prek
Apr 2, 2026
Merged

replace pre-commit with prek#402
Theoreticallyhugo merged 2 commits into
mainfrom
feat/switch-to-prek

Conversation

@Theoreticallyhugo
Copy link
Copy Markdown
Contributor

@Theoreticallyhugo Theoreticallyhugo commented Mar 30, 2026

pre-commit is a slow relict of the past. [prek](https://github.com/j178/prek) is a drop in replacement that is much faster at setting up the hooks on the first run, and it supports nice features beyond pre-commits capabilities.


HOW-TO: migrating from pre-commit to prek
Whenever you would've run a command like uv run pre-commit run -a, just replace pre-commit with prek.
Before each commit, i would reccommend running uv run prek run -a to make sure that the cicd will be passing.
If you want to run prek on each commit to make sure you are not pushing something that fails the cicd, install prek as git hook:
uv run prek install -f
That way git will run prek on each commit, and prevent commits that fail prek.

If you want to add a new hook to prek, take into consideration when it should run and set the priority appropriately.
Hooks run in the order of their priority, starting at 0. hooks with the same priority run simultaneously.
@amisfar @leonhardhennig


main result:

  • prek needs no dependencies.
  • preks initial setup is about four times as fast.

details:

  • This PR removed the pre-commit package and added prek to our environment. This reduces the amount of dependecies in our uv.lock. (prek has no dependencies)
  • This PR added priority to the pre-commit hooks. This allows them to run simultaneously. For more clarity in the priority system, i have reordered some of the hooks. I did not change which hooks we are running, I just changed the order. Sadly that makes the git diff look quite confusing.
  • The prek git hook is helpful. (But so was the one of pre-commit. no change here. I just wasn't aware of it)
  • prek supports separate workspaces and other features for larger monorepos which we might want to look into in the future.
  • prek is a drop in replacement. Adjustments were made to improve the performance further, not because compatability would have required them.
  • The documentation regarding pre-commit in our main readme has been adapted to prek.
  • The make precommit command has been adapted to call prek instead.
  • The cicd pipeline has been adapted to use prek.

@ArneBinder
Copy link
Copy Markdown
Contributor

How much faster is it (before vs. after runtime comparison)?

@Theoreticallyhugo
Copy link
Copy Markdown
Contributor Author

How much faster is it (before vs. after runtime comparison)?

there are multiple things to measure here.

local runs without cached venvs (first run):

  • pre-commit: 97sec
  • prek: 25sec

local runs with cached venvs (any but the first run):

  • pre-commit: 12sec
  • prek: 11sec

I'm not sure how much faster/ slower my laptop is than the github actions machine, but it probably wont make a massive difference for cicd, as we are using cached runs.

however, prek supports running hooks in parallel, which i've added conservatively. this does tend to give a small speedup.

@ArneBinder
Copy link
Copy Markdown
Contributor

hmm prek on CI seems to take 160s... see here

@Theoreticallyhugo
Copy link
Copy Markdown
Contributor Author

hmm prek on CI seems to take 160s... see here

lets take the logs you are referring to apart:
the first minute is spent on setting up the .venv. this is pretty exactly a minute on each and every run. to address that i made #403
the logs of the prek run look like this:

Mon, 30 Mar 2026 15:54:06 GMT Installed 307 packages in 2.33s  # this is not prek yet, but the last step of uv setting up the .venv
Mon, 30 Mar 2026 15:54:07 GMT trim trailing whitespace.................................................Passed
Mon, 30 Mar 2026 15:54:07 GMT fix end of files.........................................................Passed
Mon, 30 Mar 2026 15:54:07 GMT check docstring is first.................................................Passed
Mon, 30 Mar 2026 15:54:08 GMT check yaml...............................................................Passed
Mon, 30 Mar 2026 15:54:08 GMT debug statements (python)................................................Passed
Mon, 30 Mar 2026 15:54:08 GMT detect private key.......................................................Passed
Mon, 30 Mar 2026 15:54:09 GMT check that executables have shebangs.....................................Passed
Mon, 30 Mar 2026 15:54:09 GMT check toml...............................................................Passed
Mon, 30 Mar 2026 15:54:09 GMT check for case conflicts.................................................Passed
Mon, 30 Mar 2026 15:54:10 GMT check for added large files..............................................Passed
Mon, 30 Mar 2026 15:54:18 GMT black....................................................................Passed
Mon, 30 Mar 2026 15:54:18 GMT nbqa-black...............................................................Passed
Mon, 30 Mar 2026 15:54:19 GMT isort....................................................................Passed
Mon, 30 Mar 2026 15:54:19 GMT nbqa-isort...............................................................Passed
Mon, 30 Mar 2026 15:54:20 GMT pyupgrade................................................................Passed
Mon, 30 Mar 2026 15:54:22 GMT flake8...................................................................Passed
Mon, 30 Mar 2026 15:54:22 GMT nbqa-flake8..............................................................Passed
Mon, 30 Mar 2026 15:54:23 GMT nbstripout...............................................................Passed
Mon, 30 Mar 2026 15:54:23 GMT mdformat.................................................................Passed
Mon, 30 Mar 2026 15:55:53 GMT bandit...................................................................Passed
Mon, 30 Mar 2026 15:55:53 GMT codespell................................................................Passed
Mon, 30 Mar 2026 15:55:53 GMT mypy (uv)................................................................Passed

prek took about 106 sec
prek starting up takes no more than a second. from then onwards there is little that prek can do to speed up the process, as it's not doing much besides starting the hooks.
generally mypy is our bottleneck and takes the longest to process, so i try to run other, non-clashing hooks simultaneously. this could probably be optimised in some way.

comparing this to a pre-commit run, we can see that, as i mentioned before, when the hooks are installed, they both run at very similar speeds.
here, pre-commit took about 101 sec, which i would consider about equal.

Mon, 30 Mar 2026 14:27:57 GMT Installed 312 packages in 3.72s
Mon, 30 Mar 2026 14:27:58 GMT trim trailing whitespace.................................................Passed
Mon, 30 Mar 2026 14:27:58 GMT fix end of files.........................................................Passed
Mon, 30 Mar 2026 14:27:59 GMT check docstring is first.................................................Passed
Mon, 30 Mar 2026 14:27:59 GMT check yaml...............................................................Passed
Mon, 30 Mar 2026 14:27:59 GMT debug statements (python)................................................Passed
Mon, 30 Mar 2026 14:27:59 GMT detect private key.......................................................Passed
Mon, 30 Mar 2026 14:27:59 GMT check that executables have shebangs.....................................Passed
Mon, 30 Mar 2026 14:27:59 GMT check toml...............................................................Passed
Mon, 30 Mar 2026 14:27:59 GMT check for case conflicts.................................................Passed
Mon, 30 Mar 2026 14:28:00 GMT check for added large files..............................................Passed
Mon, 30 Mar 2026 14:28:03 GMT black....................................................................Passed
Mon, 30 Mar 2026 14:28:03 GMT isort....................................................................Passed
Mon, 30 Mar 2026 14:28:04 GMT pyupgrade................................................................Passed
Mon, 30 Mar 2026 14:28:05 GMT flake8...................................................................Passed
Mon, 30 Mar 2026 14:28:07 GMT bandit...................................................................Passed
Mon, 30 Mar 2026 14:28:07 GMT mdformat.................................................................Passed
Mon, 30 Mar 2026 14:28:08 GMT codespell................................................................Passed
Mon, 30 Mar 2026 14:28:08 GMT nbstripout...............................................................Passed
Mon, 30 Mar 2026 14:28:10 GMT nbqa-black...............................................................Passed
Mon, 30 Mar 2026 14:28:10 GMT nbqa-isort...............................................................Passed
Mon, 30 Mar 2026 14:28:11 GMT nbqa-flake8..............................................................Passed
Mon, 30 Mar 2026 14:29:38 GMT mypy (uv)................................................................Passed

conclusion:

i personally would recommend switching to prek.

  • the setup is about a 4x speedup, though we don't need to run the setup often.
  • we have less dependencies.
  • the runs could potentially be sped up further by parallelizing the hooks.
  • running uv run prek install -f is cool to not commit things and then remember the cicd and immediately push another commit to fix pre-commit...

@ArneBinder
Copy link
Copy Markdown
Contributor

Thanks for the detailed breakdown! I'm close to be convinced.

just:

running uv run prek install -f is cool to not commit things and then remember the cicd and immediately push another commit to fix pre-commit...

What do you mean by "and then remember the cicd and immediately push another commit to fix pre-commit"?

Also, we would need to have a (very) short "migration guide" (I guess in this PR description is fine). And the arguments you list above should also go into the main PR description.

And the main readme needs to be adjusted.

@Theoreticallyhugo
Copy link
Copy Markdown
Contributor Author

Thanks for the detailed breakdown! I'm close to be convinced.

just:

running uv run prek install -f is cool to not commit things and then remember the cicd and immediately push another commit to fix pre-commit...

What do you mean by "and then remember the cicd and immediately push another commit to fix pre-commit"?

Also, we would need to have a (very) short "migration guide" (I guess in this PR description is fine). And the arguments you list above should also go into the main PR description.

And the main readme needs to be adjusted.

this is probably just me being silly, but i have a tendency to forget running pre-commit before committing and pushing on about a quarter of my commits or so. whenever that happens, cicd fails because some hook found some left over whitespace somewhere, and i need to run pre-commit locally, commit and push again and then the cicd passes.

if you do the prek install -f, git will run prek whenever you're trying to commit, and git will only commit if prek passes. this can be annoying, for sure, but it also makes sure that all commits come with a stamp of approval and all cicd runs pass (at least the pre-commit half)

@ArneBinder
Copy link
Copy Markdown
Contributor

if you do the prek install -f, git will run prek whenever you're trying to commit, and git will only commit if prek passes. this can be annoying, for sure, but it also makes sure that all commits come with a stamp of approval and all cicd runs pass (at least the pre-commit half)

ah ok. so the same as installing the pre-commit git hook

@Theoreticallyhugo Theoreticallyhugo marked this pull request as ready for review March 31, 2026 10:42
@Theoreticallyhugo
Copy link
Copy Markdown
Contributor Author

And the main readme needs to be adjusted.

I had adjusted the main readme yesterday. please let me know whether that would do it, or what you think is lacking/ wrong.

@ArneBinder
Copy link
Copy Markdown
Contributor

I had adjusted the main readme yesterday. please let me know whether that would do it, or what you think is lacking/ wrong.

ah, sorry, you are right. it is fine like that

Copy link
Copy Markdown
Contributor

@ArneBinder ArneBinder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor remarks below regarding the PR description.

General: Please check the diff view on GitHub and ensure that all changes are documented in the PR description. And use real sentences in the PR description please (don't make it harder to read than necessary).

When this is merged, please cc all relevant devs of this project (I guess Leo and Amir are sufficient) at the end of the PR description so that they are aware of this change. And also mention it in our next DFKI-internal meeting to be double-sure.

Comment thread .pre-commit-config.yaml
Comment thread .pre-commit-config.yaml
@Theoreticallyhugo
Copy link
Copy Markdown
Contributor Author

Just some minor remarks below regarding the PR description.

General: Please check the diff view on GitHub and ensure that all changes are documented in the PR description. And use real sentences in the PR description please (don't make it harder to read than necessary).

When this is merged, please cc all relevant devs of this project (I guess Leo and Amir are sufficient) at the end of the PR description so that they are aware of this change. And also mention it in our next DFKI-internal meeting to be double-sure.

I've edited the pr description. is it proper sentence enough or would you like me to properify it (and how)?

@ArneBinder
Copy link
Copy Markdown
Contributor

is it proper sentence enough or would you like me to properify it (and how)?

My primary concern was/is that I think sentences should start with a capital letter.

@Theoreticallyhugo
Copy link
Copy Markdown
Contributor Author

is it proper sentence enough or would you like me to properify it (and how)?

My primary concern was/is that I think sentences should start with a capital letter.

is it proper sentence enough or would you like me to properify it (and how)?

My primary concern was/is that I think sentences should start with a capital letter.

Ah, interesting point. I can do that for you 👍

@Theoreticallyhugo
Copy link
Copy Markdown
Contributor Author

Any other comments, or shall we merge?

@ArneBinder
Copy link
Copy Markdown
Contributor

Any other comments, or shall we merge?

Looks good. Thanks for the adjustments! :-)

@Theoreticallyhugo Theoreticallyhugo merged commit 07d1e88 into main Apr 2, 2026
2 checks passed
@Theoreticallyhugo Theoreticallyhugo deleted the feat/switch-to-prek branch April 2, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants