You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Checking type[C] against a Protocol incorrectly makes instances of C assignable to it (non-deterministic behaviour) #22579
When a class object (for example) type[C] is accepted as satisfying a Protocol, mypy later accepts an instance of C for the same Protocol, even where the instance does not satisfy the Protocol. The result depends on the order of the checks: in a file, on whether the class object's check comes first, and across files, on the order mypy checks the files in.
To Reproduce
fromtypingimportProtocolclassC:
passclassMakesC(Protocol):
def__call__(self) ->C: ...
# Bug: commenting class_object() out makes instance()'s body an error (as it should be).defclass_object(c: type[C]) ->None:
m: MakesC=c# OK: calling the class makes a C# The assignment to m is not reported as an error when class_object() exists.definstance(c: C) ->None:
m: MakesC=c# error expected: a C isn't callable
main.py:19: error: Incompatible types in assignment (expression has type "C", variable has type "MakesC") [assignment]
which is what mypy reports if class_object is removed, or moved after instance.
Actual Behaviour
Success: no issues found in 1 source file
Across files: with C and MakesC in shapes.py, the class object's assignment in z_class_object.py and the instance's in a_instance.py, mypy a_instance.py z_class_object.py reports nothing, and mypy z_class_object.py a_instance.py reports the error.
Your Environment
Mypy version used: 2.4.1 (compiled)
Mypy command-line flags: none (also with --no-incremental)
Mypy configuration options from mypy.ini (and other config files): python_version = 3.14
Python version used: 3.14
LLM speculation about cause (I've not attempted to verify)
Cause
SubtypeVisitor.visit_type_type checks type[C] against the Protocol with is_protocol_implementation(C, MakesC, class_obj=True). When that succeeds, is_protocol_implementation ends with
recording (C, MakesC) with a subtype kind that doesn't include class_obj. When the instance C is checked, visit_instance finds the entry with type_state.is_cached_subtype_check(...) and accepts, without checking C's members.
This looks like the same family as #22151 (a check with skip=["__call__"] recording a full positive entry; PRs #22153, #22154). Not recording an entry when class_obj is true, as those PRs do for a non-empty skip, would fix this too. Alternatively, class_obj could be part of the cache key.
Activity