Fix infinite recursion in protocol subtype check (GH#21739)#21750
Open
mangodxd wants to merge 1 commit into
Open
Fix infinite recursion in protocol subtype check (GH#21739)#21750mangodxd wants to merge 1 commit into
mangodxd wants to merge 1 commit into
Conversation
When checking if a class implements a protocol, the 'assuming' mechanism tracks (left, right) pairs to detect cycles. But when the left type keeps growing (e.g. Ref[T] -> Exp[Ref[T], T] -> Exp[Exp[Ref[T], T], Any] -> ...), the exact-match check never triggers. Add a guard: if the assuming stack exceeds 50 entries for the same protocol, bail out assuming compatibility. This prevents infinite recursion while preserving correct behavior for all normal cases. Closes python#21739
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.
Description
Fixes #21739
Problem
When a generic class references itself in a method's return type via a protocol, mypy enters an infinite loop between
is_callable_compatibleandis_protocol_implementation.The reproducer:
Root Cause
The
assumingmechanism inis_protocol_implementationtracks(left, right)pairs to detect recursive protocol checks. When it encounters the same pair again, it returnsTrue(assuming compatibility).However, in this case the
lefttype keeps growing:Ref[T]→Exp[Ref[T], T]→Exp[Exp[Ref[T], T], Any]→ ... Therightprotocol stays the same, but the exact-match check onl == leftnever triggers because eachleftis a differentInstancetype.Fix
Add a guard to
is_protocol_implementation: if theassumingstack exceeds 50 entries (meaning we're deep in recursive protocol checks for the same protocol), bail out assuming compatibility. This prevents infinite recursion while preserving correct behavior for all normal cases.Tests
Added
testProtocolInfiniteRecursionGrowingLeftincheck-protocols.testusing a simplified version of the reproducer from the issue.