Repository navigation
Conversation
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
force-pushed
the
fix/protocol-class-object-cache
branch
from
December 1, 2026 11:44
48c35bb to
06ef064
Compare
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, sotype[C]can legitimately satisfy a protocol withdef __call__(self) -> C. That result was cached under the same subtype kind that plain instance checks use, so a laterCvs 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):Moving
instance()aboveclass_object()reports the error, so the two answers disagree. With this commit the error is reported in both orders:The fix is to include
class_objinSubtypeVisitor.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 everytype[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 withinstance()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: