Skip to content

Checking type[C] against a Protocol incorrectly makes instances of C assignable to it (non-deterministic behaviour) #22579

Backlog

Description

@h4l

Bug Report

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

from typing import Protocol


class C:
    pass


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


# Bug: commenting class_object() out makes instance()'s body an error (as it should be).
def class_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.
def instance(c: C) -> None:
    m: MakesC = c  # error expected: a C isn't callable

mypy Playground

Expected Behaviour

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

type_state.record_subtype_cache_entry(subtype_kind, left, right)

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

  1. added
    bug
    on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions