Skip to content

Don't reuse class object protocol checks for instance checks - #22581

Open
ege-arhan wants to merge 1 commit into
python:masterfrom
ege-arhan:fix/protocol-class-object-cache
Open

ege-arhan wants to merge 1 commit into
python:masterfrom
ege-arhan:fix/protocol-class-object-cache

Conversation

@ege-arhan

@ege-arhan ege-arhan commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes #22579.

is_protocol_implementation() always writes a positive subtype cache entry when the check succeeds. For a class object check (class_obj=True) the __call__ member is resolved against the constructor, so type[C] can legitimately satisfy a protocol with def __call__(self) -> C. That result was cached under the same subtype kind that plain instance checks use, so a later C vs protocol check hit the cached entry and returned True without looking at __call__ on the class at all. That is why the reported error appears or disappears depending on which check runs first.

Repro on master (601fcf8):

$ cat main.py
from typing import Protocol

class C:
    pass

class MakesC(Protocol):
    def __call__(self) -> C: ...

def class_object(c: type[C]) -> None:
    m: MakesC = c

def instance(c: C) -> None:
    m: MakesC = c
$ python -m mypy --no-incremental main.py
Success: no issues found in 1 source file

Moving instance() above class_object() reports the error, so the two answers disagree. With this commit the error is reported in both orders:

main.py:13: error: Incompatible types in assignment (expression has type "C", variable has type "MakesC")  [assignment]
main.py:13: note: "C" is missing following "MakesC" protocol member:
main.py:13: note:     __call__

The fix is to include class_obj in SubtypeVisitor.build_subtype_kind(), so class object checks and instance checks keep separate cache entries instead of one poisoning the other. I did not drop the cache for class object checks; that worked too but throws away reuse for every type[X] check.

One test case added in check-protocols.test (testProtocolClassObjectCallableDoesNotAffectInstanceCheck, listed order). It fails on master with no output for the second assignment and passes with the change. I also checked the same file with instance() listed first; that ordering already passes on master because no cache entry exists yet, so I did not add it as a regression test.

Tests run:

pytest mypy/test/testcheck.py::TypeCheckSuite::check-protocols.test   # 252 passed
pytest mypy/test/testcheck.py                                        # 8341 passed, 45 skipped, 7 xfailed
mypy --config-file mypy_self_check.ini mypy/subtypes.py               # Success
black --check mypy/subtypes.py                                        # unchanged
ruff check mypy/subtypes.py                                           # All checks passed
is_protocol_implementation() records a positive subtype cache entry at the
end. When the check was made for a class object (class_obj=True), __call__ is
resolved against the constructor signature instead of the instance attribute,
but the cache kind did not record that difference. A later plain instance
check of the same class against the same protocol then hit the cached entry
and was accepted, which made the result depend on check order.

Pass class_obj into build_subtype_kind() so the two kinds of check get
separate cache entries.
@ege-arhan
ege-arhan force-pushed the fix/protocol-class-object-cache branch from 48c35bb to 06ef064 Compare December 1, 2026 11:44
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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

Labels

None yet

1 participant