question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`pre-commit run mypy` has a different behavior than `mypy` regarding giving preference to `.pyi` files over `.py`

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
sziemcommented, Nov 12, 2021

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.

  - repo: local
    hooks:
      - id: mypy-local
        name: mypy
        entry: mypy
        language: python
        types: [python]
        files: '.*\.py[i]?'
        exclude: '/path/to/file_that_exists_as_both_py_and_pyi.py'
        # args: ["--ignore-missing-imports", "--scripts-are-modules"] (add if you use them)
        require_serial: true

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 the py 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

3reactions
s-weigandcommented, Nov 29, 2020

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 using pass_filenames: false you prevent pre-commit from passing the staged files to mypy. Hope this helps someone 😄

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.790
    hooks:
      - id: mypy
        exclude: ^docs
        args: ["--ignore-missing-imports", "--scripts-are-modules", <project_source_dir>]
        pass_filenames: false

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?

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found