Skip to content

Don't cache partial protocol implementation checks - #22154

Open
atty57 wants to merge 1 commit into
python:masterfrom
atty57:fix/partial-protocol-subtype-cache
Open

atty57 wants to merge 1 commit into
python:masterfrom
atty57:fix/partial-protocol-subtype-cache

Conversation

@atty57

@atty57 atty57 commented Oct 9, 2026

Copy link
Copy Markdown

Fixes #22151.

is_protocol_implementation(..., skip=[...]) checks only a subset of protocol members, but cached successful checks as full subtype results. During inference against a union of callback protocols, skipping __call__ could therefore pollute the cache for builtins.function, causing a later call to infer Never depending on which call was checked first.

Only record a subtype cache entry when no members were skipped. Add data-driven regression tests for both call orders; both fail on the original code and infer the expected int and str with this change.

Validation on Windows with Python 3.11.9:

  • python runtests.py self self-packaging: passed.
  • python runtests.py lint: all 13 hooks passed.
  • python runtests.py pytest-fast pytest-cmdline pytest-slow: 13,171 tests passed after the retry below, with 680 skipped, 9 expected failures, and 46 subtests passed.
  • One daemon test encountered a sandbox restriction on Windows taskkill; python runtests.py testDaemonStatusKillRestartRecheck passed when retried with normal process permissions.
  • git diff --check: passed.

Partial protocol checks can skip incompatible members and therefore must
not populate the full subtype cache. Add regression tests for inference
against a union of callback protocols in both call orders.

Fixes python#22151.
@github-actions

github-actions Bot commented Oct 9, 2026

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.

Type variable inferred as Never for a union of callback protocols, depending on earlier calls (causes flakiness)

1 participant