chore: add mypy to pre-commit
closes #1912
Description
mypy was missing from .pre-commit-config.yaml despite pyproject.toml
already pinning mypy==1.4.1 with a comment saying the versions should
stay in sync. mirrors-mypy is not viable here because torch cannot be
listed as an additional_dependency — the hook's isolated environment has
no access to torch stubs, causing hundreds of cascading false errors. A
repo: local hook with language: system runs mypy inside the project's
venv instead, matching exactly what make type-check already does.
Changes:
- Add mypy hook to
.pre-commit-config.yamlwithlanguage: system,pass_filenames: false, andargs: [lightly, tests] - Add
types-PyYAMLto[dev]deps inpyproject.toml(required because the new test importsyaml)
Documentation
- [x] I have updated the documentation.
Tests
- [x] I have updated the tests.
Added test_pre_commit_config__mypy_hook_config to
tests/test_requirements.py — asserts the hook is present, uses
language: system, sets pass_filenames: false, and passes
lightly/tests as targets.
Verified on latest upstream master (fd1eee44):
make format-check # clean
make type-check # no issues found in 533 source files
pytest tests/test_requirements.py -v # 3 passed
pre-commit run mypy --all-files # passed
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 86.46%. Comparing base (f4b4d91) to head (81fac1f).
:warning: Report is 1 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #1947 +/- ##
=======================================
Coverage 86.46% 86.46%
=======================================
Files 170 170
Lines 7271 7271
=======================================
Hits 6287 6287
Misses 984 984
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Thank you for the nice work!
I tested on my machine and it worked perfectly! The only problem is that mypy is so painfully slow haha.
I was thinking that maybe hooking into pre-push instead of pre-commit can be a better dev experience. Do you have any opinion?
Thanks for testing it out and for the feedback!
Hooking it into pre-push sounds like a good compromise: we still enforce type checks before code is pushed, but keep the inner dev loop fast.
Would it be better to enable the pre-commot bot so it is tested on each PR?
Good question! A couple of things:
mypy already runs on every PR — test_code_format.yml runs make type-check (Python 3.7 + 3.12, full env), so PRs are covered.
And pre-commit.ci can't actually run this hook: it needs the project env (mypy + torch), which isn't available in the bot's sandbox — same reason mirrors-mypy didn't work here. Plus it's a pre-push hook, which the bot doesn't run. So the local hook stays a fast convenience while CI does the enforcing.
Hi @Borda,
Thanks for the suggestion, that’s a good point!
In this case, I don’t think pre-commit.ci would help much with this particular hook. The mypy check needs to run inside the project environment because of dependencies like torch, which is why we use a
local language: system hook instead of mirrors-mypy.
Also, since we moved this to pre-push to avoid slowing down every commit, it is mainly intended as a local developer check before pushing.
The actual enforcement still happens in CI via make type-check. The local hook is mostly there to give contributors faster feedback before pushing or updating a PR, especially when workflows from external
contributors need approval before they can run.
Thank you for the contribution @saud5150 !!