Dirich
Dirich

Reputation: 442

mypy overrides in toml are ignored?

The following is a simplified version of the toml file example from the mypy documentation:

[tool.mypy]
python_version = "3.7"
warn_return_any = true
warn_unused_configs = true

[[tool.mypy.overrides]]
module = ["somelibrary"]
ignore_missing_imports = true

I am using this configuration in a project where I have a third party library (here named "somelibrary") that is missing type hints and thus causes a lot of spam in the mypy report.

The global mypy configuration is picked up, so I know the overall setup is fine, but whenever I switch ignore_missing_imports in the somelibrary override to true, mypy still behaves as if I had left it to false (I used this same option in the global mypy configuration to make sure to things worked the expected way, which is the case).

I'm using the (currently) latest mypy version, 0.931.

Am I doing something wrong? Is mypy bugged? Something else?

Upvotes: 8

Views: 7773

Answers (2)

discgolfdave
discgolfdave

Reputation: 31

You can try running Mypy from the command line in this way: mypy --no-incremental src

Mypy uses a caching feature to speed up the process of generating results from previously evaluated code modules. This is mostly done for processing speed, and mypy skips re-evaluating code that hasn't changed. Unfortunately, my experience is that mypy often needs to re-evaluate all code when the global configuration settings change, and it's not set up to do this. This is why you can run with different settings and get the same result.

It is also possible to disable caching with a global mypy setting in pyproject.toml. It works like this:

[tool.mypy]
no_incremental = true

I prefer the command line flag because this way the speed of mypy in pipelines is as fast as possible.

More info on mypy's caching comes from mypy --help:

Incremental mode:
  Adjust how mypy incrementally type checks and caches modules. Mypy caches type information about modules into a cache to let you speed up future invocations of mypy. Also see mypy's daemon mode:     
  mypy.readthedocs.io/en/stable/mypy_daemon.html#mypy-daemon

  --no-incremental          Disable module cache (inverse: --incremental)
  --cache-dir DIR           Store module cache info in the given folder in incremental mode (defaults to '.mypy_cache')
  --sqlite-cache            Use a sqlite database to store the cache (inverse: --no-sqlite-cache)
  --cache-fine-grained      Include fine-grained dependency information in the cache for the mypy daemon
  --skip-version-check      Allow using cache written by older mypy version
  --skip-cache-mtime-checks
                            Skip cache internal consistency checks based on mtime

Upvotes: 0

Jens
Jens

Reputation: 311

You should add your project to overrides. Lets assume your project is called "my_project" and your library/module "somelibrary" is directly under your "my_project" folder. The toml file should look like this:

[tool.mypy]
python_version = "3.7"
warn_return_any = true
warn_unused_configs = true

[[tool.mypy.overrides]]
module = "my_project.somelibrary"
ignore_missing_imports = true

Upvotes: 9

Related Questions