Skip to content

Cache sometimes gets ignored for unchanged files #22159

Open

Description

@A5rocks

Bug Report

I ran into a case where the cache would be marked stale on the second run, despite files not changing!

To Reproduce

This was minimized by an LLM, the original case is significantly more complicated. Additionally, it may still not be fully minimized. The directory structure looks like this:

├── a.py
├── b.py
├── e.py
└── site
    └── ns
        └── sub
            ├── __init__.py
            └── py.typed

and the contents are:

# a.py
import ns.missing
import os

y = os.environ
# b.py
import ns.sub
# e.py
import b

site/ns/sub/__init__.py and site/ns/sub/py.typed are empty.

Expected Behavior

Second run doesn't need to do any work and just reads from cache.

Actual Behavior

Running PYTHONPATH=site uv run mypy -v a.py e.py 2>&1 | rg 'Scheduling SCC', the first run gets:

LOG:  Scheduling SCC of size xxx (xxx) as inherently stale
LOG:  Scheduling SCC singleton (ns) as inherently stale
LOG:  Scheduling SCC singleton (ns.sub) as inherently stale
LOG:  Scheduling SCC singleton (a) as inherently stale
LOG:  Scheduling SCC singleton (b) as inherently stale
LOG:  Scheduling SCC singleton (e) as inherently stale

and the second run gets:

LOG:  Scheduling SCC singleton (a) as inherently stale

Your Environment

  • Mypy version used: 2.4.0
  • Mypy command-line flags: -v
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12.13

Activity

  1. Shanmukh-l33t commented on Nov 11, 2026

    @Shanmukh-l33t

    A tested fix for this is available if it is useful for fold-in: https://github.com/Shanmukh-l33t/mypy/tree/fix-22159-stale-cache-a (commit 4d68fe37d2b213fa5f2b937f7aa850c5cf61ddd1, previously #22403 — closed unmerged, no action needed there).

    Root cause: load_graph()'s up-suppress pass re-adds a suppressed dependency via add_dependency(), which appends it at the end of State.dependencies. That reorders the list without changing its membership, and State.is_fresh() compares the dependency list order-sensitively against the cache — so on a warm run the unchanged module looks "changed" and the cache is ignored (the symptom in the report).

    Fix: compare the dependency lists as sets in State.is_fresh() (mypy/build.py, +11/−1) with a regression test in mypy/test/testcache.py (+82). Evidence: the test reproduces the spurious rebuild at the parent commit (RED) and passes with the fix (GREEN); full suite 12,534 passed with one failure proven pre-existing on the parent tree; black and ruff clean.

    Happy to rebase it onto current main or split out just the regression test if that helps.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions