mypy icon indicating copy to clipboard operation
mypy copied to clipboard

False positives about non-existing import when using mypy cache

Open VictorEngmarkHexagon opened this issue 1 month ago • 3 comments

Bug Report

I'm getting a bunch of false positives when reusing .mypy_cache in CI and refactoring the code base. They disappear after clearing the CI mypy cache, so they must be related to the cache.

To Reproduce

I'm not sure about the exact steps. This is what I've gathered so far:

  1. Run mypy on an existing project
  2. Rename some module
  3. Re-run mypy

I was able to reproduce the "reverse" of the original issue locally in the repo (closed source) by running mypy in a branch containing a refactor, switching back to the default branch, and re-running mypy. I then got the same type of errors, but complaining that the modules in the refactor branch could not be found.

Only the first run of mypy fails. Subsequent runs "understand" that some code has been renamed, and no longer produces any errors.

Expected Behavior

The code should pass validation on the first try without having to clear the cache.

Actual Behavior

some/path/__init__.py:1: error:
Cannot find implementation or library stub for module named
"project.module.submodule"  [import-not-found]
    from collections import defaultdict
    ^

Clearly the built-in collections module import doesn't depend on a project-internal module, so this error message is nonsensical.

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): pyproject.toml.txt
  • Python version used: 3.12.11

VictorEngmarkHexagon avatar Nov 26 '25 10:11 VictorEngmarkHexagon

@sterliakov Does this sound like something fixed by https://github.com/python/mypy/pull/19044?

VictorEngmarkHexagon avatar Nov 26 '25 12:11 VictorEngmarkHexagon

Could you upgrade to latest mypy==1.18.2 (or, ideally, install from master with pip install git+https://github.com/python/mypy - this installs a slow, non-compiled version, but contains all latest changes) and retry your experiment, please?

This could be fixed by #19044, but that's not very likely unless your repository contains some namespace packages (those without __init__.py). However, there have been other cache/incremental updates since 1.15.0, so your issue might be fixed already!

sterliakov avatar Nov 26 '25 13:11 sterliakov

I don't have time to do the update at the moment, so I've disabled caching for now. Will get back to this ticket later.

VictorEngmarkHexagon avatar Nov 26 '25 13:11 VictorEngmarkHexagon

Thank you @sterliakov, upgrading to 1.17.1 fixed it!

VictorEngmarkHexagon avatar Dec 16 '25 14:12 VictorEngmarkHexagon