python / python/mypy

Using mypy via pre-commit

Open
#13,916 14 comments 22 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug documentation priority-0-high topic-incremental topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

pre-commit is useful; many mypy users use mypy via pre-commit. This issue exists so I can link to it when users run into common gotchas. Please do not discuss pre-commit related feature requests here, open a new issue.

pre-commit runs mypy in an isolated environment

pre-commit wants checks to run in isolated environments. mypy wants to know as much about your program as it can, including everything about everything you import. These are in conflict.

The pre-commit mypy mirror thing automatically (and to most users, surprisingly) passes --ignore-missing-imports, which means that most types originating from third party dependencies will become Any: https://github.com/pre-commit/mirrors-mypy/blob/33f4a30be4e66513c51a857712fc732e1a9ddd78/.pre-commit-hooks.yaml#L7
In my opinion, this is really bad: it will silently make type checking far less effective. It also means you'll likely get different results when running mypy directly.

Two possible pre-commit fixes in your pre-commit config:

  1. Use language: system (and maybe a helper script) to run something from outside a pre-commit environment
  2. Repeat your list of dependencies (and stub dependencies) in additional_dependencies
pre-commit passes only diffed files

This is sort of the core idea of pre-commit and for most pre-commit checks, this is great. But mypy really wants to do whole program analysis, and will try to follow all imports, so pre-commit is not actually saving you any work. In fact, if it is saving you work, it's probably undesirable and some bug in your mypy setup. Anyway, this is another source of potential differences between running mypy directly, e.g. there are undesirable interactions with --exclude: mypy assumes that if you pass a file in the command, you want it to be checked.

Recommended pre-commit fix:
In your pre-commit config, use pass_filenames: false

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No mypy repository file or test is named. Start by reviewing the documented pre-commit behavior and the linked .pre-commit-hooks.yaml, then seek maintainer guidance on the intended documentation location and scope before making changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.