Skip to content

Report a clean error for corrupt cache instead of INTERNAL ERROR - #22223

Open
inchang-ing wants to merge 2 commits into
python:masterfrom
inchang-ing:fix/cache-corrupt-message
Open

inchang-ing wants to merge 2 commits into
python:masterfrom
inchang-ing:fix/cache-corrupt-message

Conversation

@inchang-ing

Copy link
Copy Markdown
Contributor

Summary

When the mypy cache is corrupt (for example, partially overwritten in a shared/temp directory), mypy used to crash with an INTERNAL ERROR. This PR replaces that with an actionable error telling the user to remove the cache directory (or pass a different --cache-dir) and re-run.

Fixes #22220

How it works

The fix adds guards on every cache-read path in mypy/build.py:

  • Deserializing the cache data file (State.load_tree) — the most common corruption case (a garbage/trucated .data.ff).
  • Resolving cache metadata (CacheMeta.read / CacheMeta.deserialize and CacheMetaEx).
  • Fixing cross-references after deserialization (State.fix_cross_refs), where a missing type reference used to raise during fix-up.
  • The deferred "TypeInfo not fixed" AssertionError (NOT_READY placeholder) that otherwise surfaced deep inside type checking, caught in State.wrap_context.

All paths now report a blocker error such as:

<cache>.data.ff: error: Error reading cache file; you likely have a corrupt cache.
Try removing the .mypy_cache directory and run mypy again.

Reproduction / verification

Before the fix, a corrupt cache produces:

error: INTERNAL ERROR -- Please try using mypy master on GitHub: ...

After the fix:

dep.data.ff: error: Error reading cache file; you likely have a corrupt cache.
Try removing the .mypy_cache directory and run mypy again.
Found 1 error in 1 file (errors prevented further checking)

A new regression test (mypy/test/testcache.py) populates the cache, corrupts the cached data file (preserving mtime so the cache is still trusted), and asserts the next build fails with a clean "corrupt cache" error and without an INTERNAL ERROR. The test fails on the unguarded code (raw AssertionError / INTERNAL ERROR) and passes with the fix.

Test plan

  • python -m pytest mypy/test/testcache.py -n0 — new test passes.
  • python -m pytest mypy/test/testmerge.py -n0 — existing cache/merge tests pass (no regression).

inchang-ing and others added 2 commits October 19, 2026 10:42
When the mypy cache is corrupt (e.g. partially overwritten), mypy used
to crash with an INTERNAL ERROR. Now it reports an actionable error that
tells the user to remove the cache directory (or pass a different
--cache-dir) and re-run.

This covers corruption detected while reading/deserializing the cache
data file, while resolving cache metadata, while fixing cross-references
after deserialization, and the deferred "TypeInfo not fixed" assertion
that would otherwise surface deep inside type checking.

Fixes python#22220
@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

scipy-stubs (https://github.com/scipy/scipy-stubs)
+ tests/misc/test_submodules_empty.pyi:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

corrupt cache causes alarming message

1 participant