Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 919
Martin Thoma Avatar asked Jan 14 '20 09:01

Martin Thoma


People also ask

How do I run pre-commit automatically?

If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook.

How do you avoid pre-commit hooks?

Quick tip if you want to skip the pre-commit validations and quickly want to get a commit out there. To get your commit through without running that pre-commit hook, use the --no-verify option. Voila, without pre-commit hooks running!

How exclude from pre-commit?

Currently, you can use black option --force-exclude . As indicated in the documentation: --force-exclude TEXT Like --exclude, but files and directories matching this regex will be excluded even when they are passed explicitly as arguments.

How does pre-commit work?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.


2 Answers

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

like image 129
Anthony Sottile Avatar answered Oct 16 '22 12:10

Anthony Sottile


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.

like image 34
jli Avatar answered Oct 16 '22 13:10

jli