Repository navigation
[ty] Fix protocol methods whose receiver is annotated with the protocol - #29450
Draft
benedikt-bartscher wants to merge 1 commit into
Draft
benedikt-bartscher wants to merge 1 commit into
benedikt-bartscher wants to merge 1 commit into
Conversation
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
Typing conformance resultsNo changes detected ✅Current numbersThe 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. |
Memory usage reportMemory usage unchanged ✅ |
|
This branch has not been deployed
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.
Summary
Since #27301, ty rejects valid implementations of a protocol method whose
selfis annotated with the protocol itself:A callback protocol with
__call__(self: "Callback", ...)rejects every callable except a plain function: bound methods, classmethods, lambdas, callable instances andfunctools.partialobjects.Binding the protocol's
methodto the receiverImplementationchecksImplementationagainst the receiver annotationHasMethod, which needs the same binding again. #27301 made that binding the cachedprotocol_apply_self_with_receiverquery, 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
Selfsubstitution, 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 onmainand pass with this change. The existingty_python_semanticandtytests pass unchanged.