Skip to content

Compare dependency lists as sets in State.is_fresh() - #22403

Closed
Shanmukh-l33t wants to merge 1 commit into
python:masterfrom
Shanmukh-l33t:fix-22159-stale-cache-a
Closed

Shanmukh-l33t wants to merge 1 commit into
python:masterfrom
Shanmukh-l33t:fix-22159-stale-cache-a

Conversation

@Shanmukh-l33t

Copy link
Copy Markdown

Fixes #22159

What

A warm mypy run re-checked an unchanged module forever (every run logs Scheduling SCC singleton (...) as inherently stale for it) whenever the module's dependency list had been reordered by the suppress / up-suppress cycle: State.is_fresh() compared self.dependencies == self.meta.dependencies order-sensitively.

Why

load_graph() can suppress a dependency (State.new_state() raising ModuleNotFound, e.g. for the parent of a missing submodule of a namespace package on PYTHONPATH), and the up-suppress pass later re-adds it with add_dependency(), which appends it at the end of the list. Membership is unchanged, order is not. On a warm run the indirect dependencies from CacheMetaEx are already merged into self.dependencies, so the reordered list never matches the cached order: nothing ever changes, but nothing is ever fresh either — the module is re-analyzed on every run and the cache is effectively bypassed for it.

Change

  • mypy/build.py: compare the two dependency lists as sets. Both sides only ever contain unique elements (asserted in State.write_cache(), mypy/build.py:3687 — Duplicates in dependencies list), so set equality is exactly membership equality here, and the suppressed-then-re-added ordering no longer matters.
  • mypy/test/testcache.py: regression test built from the issue's repro — a missing submodule of a namespace package reachable via PYTHONPATH, plus the import os from the report. It asserts the cold run does schedule the module as inherently stale (the bug's precondition), and that the warm run and a third run schedule nothing as stale and replay byte-identical output.

Testing

  • RED → GREEN: the new test fails at the parent commit 5dc284074 with the reported symptom (assert "as inherently stale" not in second.stderr — the warm run logs Scheduling SCC singleton (a) as inherently stale; 1 failed) and passes with the fix (1 passed). The stash used for the RED run was popped and the working tree re-verified before committing.
  • Full suite: pytest mypy/test -q -n 2 → 12534 passed, 404 skipped, 10 xfailed, 1 failed. The single failure, test_find_sources_exclude (InvalidSourceList: There are no .py[i] files in directory '/'), reproduces identically with the fix stashed — a pre-existing environment failure in this sandbox, unrelated to this change. (Three other environment failures during the first full run — two attrs-related stubgen/daemon cases and testpep561's filelock ImportError — disappeared after installing the pinned attrs==26.1.0 / filelock==3.32.3 from test-requirements.txt; they too failed with the fix stashed.)
  • Lint: black --check mypy/build.py mypy/test/testcache.py, ruff check on the same files, and mypy --config-file mypy_self_check.ini mypy/build.py mypy/test/testcache.py all clean.

Checklist per the template: a test covers the changed behaviour; local checks above are green — CI to confirm.

load_graph() may suppress a dependency (State.new_state() raising
ModuleNotFound, e.g. for the parent of a missing submodule of a
namespace package) and the up-suppress pass later re-adds it with
add_dependency(), which appends it at the end of the list.  That
reorders the list without changing its membership, and on a warm
run the indirect dependencies from CacheMetaEx are already merged
in, so the order-sensitive comparison reported the module stale on
every run and re-checked it forever (python#22159).

Both lists only ever contain unique elements (asserted in
write_cache()), so comparing them as sets is equivalent for
membership and insensitive to ordering.
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.

Cache sometimes gets ignored for unchanged files

2 participants