Is it possible to run mypy pre-commit without making it fail?
Asked Answered
A

2

16

I would like to add the following to pre-commit for a team:

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: 'v0.720'
    hooks:
    -   id: mypy
        args: [--ignore-missing-imports]

My team is worried that this might be too strict. To have a gradual introduction, I would like this hook not to make the commit fail, but only to show the issues. Is that possible?

Ardene answered 14/1, 2020 at 9:22 Comment(0)
S
39

you can, but I wouldn't suggest it -- warning noise is likely to have your whole team ignore the entire output and the entire tool

here's how you would do such a thing (note that it has reduced portability due to bash -- mostly because the framework intentionally does not suggest this)

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.720
    hooks:
    -   id: mypy
        verbose: true
        entry: bash -c 'mypy "$@" || true' --

two pieces make this work:

  1. verbose: true always produces the output -- this option is really only intended for debugging purposes, but you can turn it on always (it can be noisy / annoying though)
  2. bash + || true -- ignore the exit code

disclaimer: I am the author of pre-commit

Sidnee answered 15/1, 2020 at 5:29 Comment(0)
I
5

Also note that you can temporarily disable hooks by setting the environment variable SKIP. For example:

SKIP=flake8 git commit -m 'fix thing - work in progress'

This is especially useful when you just want to make local "checkpoint" commits that you'll fix later.

Side note on mypy specifically: there's a potentially big issue with using mypy in a non-blocking way like this. If you allow commits with type errors to be merged, everyone else will start to see those type errors in their pre-commit checks.

When developers are making further changes, it's confusing whether the mypy errors that appear were there from before, or due to their further changes. This can be a recipe for frustration/confusion, and also for allowing further type errors to accumulate.

I think the mypy guide on using mypy with an existing codebase is pretty good advice.

If you just need to temporarily skip mypy checks so you can checkpoint your work, push a PR for initial review, or whatever, you can just do SKIP=mypy as mentioned above.

Indecency answered 13/5, 2021 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.