Skip to content

Fix cross-test mypy cache contamination - #3299

Merged
UnknownPlatypus merged 1 commit into
typeddjango:masterfrom
UnknownPlatypus:fix/per-test-mypy-cache
Apr 15, 2026
Merged

UnknownPlatypus merged 1 commit into
typeddjango:masterfrom
UnknownPlatypus:fix/per-test-mypy-cache

Conversation

@UnknownPlatypus

@UnknownPlatypus UnknownPlatypus commented Apr 12, 2026 •

Copy link
Copy Markdown
Contributor

I have made things!

Prior to this change, I was having the models_triple_circular_reference test always failing when running the full test suite with pytest -n auto while it was passing in isolation.

The issue was with pytest-mypy-plugin that was not handling correctly new mypy cache format.
The fix is to pull typeddjango/pytest-mypy-plugins#186

This is slightly slower but is expected since previously we were not doing any cache cleanup because of the bug.
I can confirm that it fixes pytest -n auto locally

Outdated investigation

Use a unique cache directory per test instead of per worker to prevent stale module entries from contaminating subsequent tests that reuse module names like "myapp".

Marking as draft for now to check if the performance impact is ok or not
The performance hit is too high (2x slower), I can fix the offending test by renaming the app but it seem brittle. Should something be patched in pytest-mypy-plugin @sobolevn ?

  ┌──────────────────────────────┬────────────────┬───────────────┬──────────────┐                                                                              
  │             Job              │ #3296 (before) │ #3299 (after) │    Change    │                                                                              
  ├──────────────────────────────┼────────────────┼───────────────┼──────────────┤                                                                              
  │ test (avg across all shards) │ ~1m55s         │ ~3m35s        │ ~1.9x slower │                   
  ├──────────────────────────────┼────────────────┼───────────────┼──────────────┤                   
  │ test-older-django (avg)      │ ~2m00s         │ ~3m41s        │ ~1.8x slower │                                                                              
  ├──────────────────────────────┼────────────────┼───────────────┼──────────────┤

AI Policy

  • I have read and agree to the AI Policy, removed any "Co-Authored-By" lines attributing coding agents, and manually reviewed the final result

@sobolevn

Copy link
Copy Markdown
Member

Should something be patched in pytest-mypy-plugin?

Do you have any ideas?

@UnknownPlatypus

Copy link
Copy Markdown
Contributor Author

Do you have any ideas?

I don't know where it appens but we would need to make sure the temporary fiels crated in tests contain a hash or the test case name

@sobolevn

Copy link
Copy Markdown
Member

Using test case name as a folder for temp files - sounds like a good idea! PR is welcome :)

UnknownPlatypus added a commit to UnknownPlatypus/pytest-mypy-plugins that referenced this pull request Apr 13, 2026
The old cleanup removed individual cache files per test file path.
This was insufficient because:

1. It only removed cache entries for exact files listed in the test,
   missing entries auto-generated by mypy or plugins (e.g. Django
   plugin injecting cross-app model dependencies).
2. It only handled the legacy JSON file-based cache, not the SQLite
   cache.db format used by mypy >= 1.14.

The new cleanup collects top-level module/package names from the
test's files and removes ALL cache entries under those names, using
both the SQLite database and legacy JSON file cleanup paths.

This prevents sequential cache contamination within a single
pytest-xdist worker when consecutive tests define modules with the
same name (e.g. "myapp") but different internal structures.

Fixes typeddjango/django-stubs#3299
UnknownPlatypus added a commit to UnknownPlatypus/pytest-mypy-plugins that referenced this pull request Apr 13, 2026
mypy >= 1.14 changed its incremental cache from .data.json/.meta.json
files to .data.ff/.meta.ff/.err.ff files. The cleanup code only knew
about the old .json suffixes, so it silently removed nothing, leaving
stale cache entries that contaminated subsequent tests sharing the
same module names (e.g. "myapp").

Add the modern .ff suffixes to the cleanup loop.

Fixes typeddjango/django-stubs#3299
@UnknownPlatypus

Copy link
Copy Markdown
Contributor Author

The issue was with the new style of mypy cache actually, cf typeddjango/pytest-mypy-plugins#186

@UnknownPlatypus
UnknownPlatypus force-pushed the fix/per-test-mypy-cache branch from 96c8c67 to 051dc2b Compare April 14, 2026 16:40
@UnknownPlatypus
UnknownPlatypus marked this pull request as ready for review April 14, 2026 16:49
@UnknownPlatypus

Copy link
Copy Markdown
Contributor Author

@sobolevn I've updated the pr description with details, seem to work nicely for me

@UnknownPlatypus
UnknownPlatypus merged commit d97823e into typeddjango:master Apr 15, 2026
51 of 54 checks passed
@UnknownPlatypus
UnknownPlatypus deleted the fix/per-test-mypy-cache branch May 2, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants