Skip to content

Using mypy via pre-commit #13916

Closed as not planned
Closed as not planned
@hauntsaninja

Description

@hauntsaninja

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 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 file 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions