Skip to content

[ty] Fix protocol methods whose receiver is annotated with the protocol - #29450

Draft
benedikt-bartscher wants to merge 1 commit into
astral-sh:mainfrom
benedikt-bartscher:ty-protocol-self-receiver-cycle
Draft

benedikt-bartscher wants to merge 1 commit into
astral-sh:mainfrom
benedikt-bartscher:ty-protocol-self-receiver-cycle

Conversation

@benedikt-bartscher

Copy link
Copy Markdown
Contributor

Summary

Since #27301, ty rejects valid implementations of a protocol method whose self is annotated with the protocol itself:

from typing import Protocol

class HasMethod(Protocol):
    def method(self: "HasMethod", value: int, /) -> object: ...

class Implementation:
    def method(self, value: int) -> None: ...

x: HasMethod = Implementation()  # error[invalid-assignment]

A callback protocol with __call__(self: "Callback", ...) rejects every callable except a plain function: bound methods, classmethods, lambdas, callable instances and functools.partial objects.

Binding the protocol's method to the receiver Implementation checks Implementation against the receiver annotation HasMethod, which needs the same binding again. #27301 made that binding the cached protocol_apply_self_with_receiver query, so the cycle is now detected there instead of in the assignability queries, which assume a recursive relation holds. The query's initial value, CallableType::bottom, is permissive when it stands for an implementation's method, but it rejects everything when it stands for the protocol's requirement. The fixed point therefore settles on "not assignable".

This seeds the query with the binding that assumes the receiver satisfies its annotation: the same Self substitution, without receiver constraints. That matches how the assignability queries treat recursive relations: the relation holds unless another part of the comparison fails.

Closes astral-sh/ty#4746

#27227 and #28671 also change how explicit receivers are bound in protocol comparisons and edit the same functions.

Test Plan

Added mdtests for a class implementing a method with a protocol-typed receiver, for callback protocols (bound method, classmethod, lambda, callable instance, partial, and an incompatible function), and for a specialized generic receiver. They fail on main and pass with this change. The existing ty_python_semantic and ty tests pass unchanged.

Binding a protocol method whose receiver is annotated with the protocol itself
checks the receiver against that protocol again, which re-enters the cached
receiver-binding query. Its cycle initial value, the bottom callable, rejects
every implementation when it stands for the protocol's requirement, so the
cycle settled on "not assignable". Start from the binding that assumes the
receiver satisfies its annotation instead, matching how other recursive
relations are evaluated.

Fixes astral-sh/ty#4746
@astral-sh-bot astral-sh-bot Bot added the ty The ty type checker label Oct 17, 2026
@astral-sh-bot

astral-sh-bot Bot commented Oct 17, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 98.24%. The percentage of expected errors that received a diagnostic held steady at 98.24%. The number of fully passing files held steady at 134/146.

@astral-sh-bot

astral-sh-bot Bot commented Oct 17, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Oct 17, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

This branch has not been deployed

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

Labels

ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Protocol methods with self annotated as the protocol reject valid implementations (regression in 0.0.65)

1 participant