Skip to content

Don't cache partial protocol checks that skip __call__ - #22153

Open
PGrayCS wants to merge 2 commits into
python:masterfrom
PGrayCS:pgraycs-fix-union-typevar-never-inference
Open

PGrayCS wants to merge 2 commits into
python:masterfrom
PGrayCS:pgraycs-fix-union-typevar-never-inference

Conversation

@PGrayCS

@PGrayCS PGrayCS commented Oct 9, 2026

Copy link
Copy Markdown

Fixes #22151

is_protocol_implementation(..., skip=["__call__"]) (used by constraint inference for a callable's builtins.function fallback) skips a member check but still recorded a positive subtype-cache entry. A later full check against the same protocol (e.g. OneArg[Any]) could then wrongly succeed through the cache, so a callable matched both items of a union of callback protocols. That produced conflicting constraints and Never, depending on evaluation order (and worker assignment with --num-workers).

The fix is to not record cache entries when skip is used, since the result is only valid for the partial check.

Tests: added testCallbackProtocolUnionInferenceOrderIndependent. It fails without the fix and passes with it. Also ran pytest -n auto mypy/test/testcheck.py -k "rotocol or Callable or Union or Infer or Generic": 1975 passed, 9 skipped, 1 xfailed.

PGrayCS and others added 2 commits October 9, 2026 21:36
…22151)

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@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