Skip to content

False positives about non-existing import when using mypy cache #20300

Closed

Description

@VictorEngmarkHexagon

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

Activity

  1. VictorEngmarkHexagon commented on Nov 26, 2025

    @VictorEngmarkHexagon
    Author

    @sterliakov Does this sound like something fixed by #19044?

  2. sterliakov commented on Nov 26, 2025

    @sterliakov
    Collaborator

    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!

  3. VictorEngmarkHexagon commented on Nov 26, 2025

    @VictorEngmarkHexagon
    Author

    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.

  4. VictorEngmarkHexagon commented on Dec 16, 2025

    @VictorEngmarkHexagon
    Author

    Thank you @sterliakov, upgrading to 1.17.1 fixed it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions