`pre-commit run mypy` has a different behavior than `mypy` regarding giving preference to `.pyi` files over `.py`
See original GitHub issueI’ve created a minimal working example here: https://github.com/tadeu/pre-commit-mypy-issue
It’s about this exact situation described in mypy
documentation:
If a directory contains both a
.py
and a.pyi
file for the same module, the.pyi
file takes precedence. This way you can easily add annotations for a module even if you don’t want to modify the source code. This can be useful, for example, if you use 3rd party open source libraries in your program (and there are no stubs in typeshed yet).
The problem is that mirrors-mypy
is passing all files explicitly in the command line, and it seems to be passing the .py
files instead of the .pyi
files in this case:
$ pre-commit run mypy -a
mypy.....................................................................Failed
- hook id: mypy
- exit code: 1
src/example/__init__.py:6: error: Function is missing a type annotation [no-untyped-def]
src/example/something.py:5: error: Call to untyped function "Example" in typed context [no-untyped-call]
Found 2 errors in 2 files (checked 2 source files)
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
pre-commit: Mypy local hook reports the same error multiple ...
This is why I am trying to use Mypy as a local hook. Currently, I simply run plain Mypy in my CI/CD as...
Read more >Mypy Documentation - Read the Docs
Mypy is a static type checker for Python. Type checkers help ensure that you're using variables and functions in your code correctly.
Read more >Running Mypy in Pre-commit - Jared Khan
This post is about running Mypy in a Git pre-commit hook using the Pre-commit framework. Running Mypy is a little fiddly in itself, ......
Read more >library stubs not installed for yaml mypy - You.com | The AI ...
A solution was found in this discussion Changing the mypy entry in the .pre-commit-config.yaml to: # Test if the variable typing is correct....
Read more >Running mypy and managing imports
Note that we can specify multiple packages and modules on the command line. ... Mypy will recursively discover and check all files ending...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I ran into this issue today after adding a
mypy-protobuf
stub file to my code.I ended up using @max-sixty 's suggestion for the file pattern plus an explicit exclude of the file duplicated by the stub file, so that only the stub file is used for type-checking by mypy.
This is feasible, if the number of stub files is small and not expected to change. If the number is large, I guess we’d need some kind of pattern to express that the
pyi
file is always to be preferred over thepy
file. An ugly workaround could be to give all files with stubs a common suffix, so that they can be excluded with a pattern like.*_suffix.py
After just adding stubs I had the same issue. My wasteful but working workaround is to just run
mypy
on the whole source folder, as I would from the command line. By usingpass_filenames: false
you preventpre-commit
from passing the staged files tomypy
. Hope this helps someone 😄My guess is that this issue boils down to a difference between how
mypy
processes folders (where it possibly swaps*.py
files for*.pyi
files) and file paths (possibly not filtering and/or looking up) as arguments.@tadeu could you please link the issue at
mypy
so I can follow it?