3

In the following MWE, I have two files/modules:

  1. main.py which is and should be checked with mypy
  2. and importedmodule.py which should not be type checked because it is autogenerated. This file is autogenerated, I don't want to add type:ignore.

MyPy Command

$ mypy main.py --exclude '.*importedmodule.*'
$ mypy --version
mypy 0.931

main.py

"""
This should be type checked
"""

from importedmodule import say_hello

greeting = say_hello("Joe")
print(greeting)

importedmodule.py

"""
This module should not be checked in mypy, because it is excluded
"""


def say_hello(name: str) -> str:
    # This function is imported and called from my type checked code
    return f"Hello {name}!"


def return_an_int() -> int:
    # ok, things are obviously wrong here but mypy should ignore them
    # also, I never expclitly imported this function
    return "this is a str, not an int" # <-- this is line 14 referenced in the mypy error message

But MyPy complains about the function that is not even imported in main.py:

importedmodule.py:14: error: Incompatible return value type (got "str", expected "int") Found 1 error in 1 file (checked 1 source file)

What is wrong about my exclude?

koks der drache
  • 1,398
  • 1
  • 16
  • 33
  • 1
    @blhsing there was one important detail missing. The imported file is autogenerated from a .proto file. Therefor it is not a good solution to add `type: ignore` at the beginning of the file. – unlimitedfox May 23 '22 at 08:53
  • 1
    You can [override](https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file) `importedmodule` in `pyproject.toml` (or whichever config file you use, maybe `mypy.ini`) and set `ignore_errors = True` for that particular module. Note that `exclude` has one more drawback: if you run `mypy` on project dir (recursively), excluded file will be checked anyway (included files have higher precedence for some reason). – STerliakov May 23 '22 at 09:34

2 Answers2

3

In order to make SUTerliakov comment on your question more visible, I would like to present it here again in more detail.

In the pyproject.toml file you can insert the following below your other mypy config

[[tool.mypy.overrides]]
module = "importedmodule"
ignore_errors = true

With this config you will ignore all errors coming from the mentioned module.

By using a wildcard, you can also ignore all modules in a directory:

[[tool.mypy.overrides]]
module = "importedpackage.*"
ignore_errors = true
Neuron
  • 5,141
  • 5
  • 38
  • 59
unlimitedfox
  • 386
  • 4
  • 8
2

From the mypy documentation,

In particular, --exclude does not affect mypy’s import following. You can use a per-module follow_imports config option to additionally avoid mypy from following imports and checking code you do not wish to be checked.

Unfortunately it doesn't seem like there is a cli option for disabling mypy for unused functions in imported modules. When importing a module, mypy analyses all of it. The static analyser doesn't check if a function is used or not.

However you can silence any errors created in those imported modules

$ mypy main.py --follow-imports silent
Success: no issues found in 1 source file
$ mypy main.py --follow-imports skip
Success: no issues found in 1 source file
AlexLoss
  • 491
  • 4
  • 13