Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,11 @@ def f(self) -> A: ...
for l, r in reversed(assuming):
if l == left and r == right:
return True
# Guard against infinite recursion when left type keeps growing but right
# protocol stays the same (GH#21739). If we're already checking this
# protocol against many different left types, bail out assuming compatibility.
if len(assuming) > 50:
return True
with pop_on_exit(assuming, left, right):
for member in right.type.protocol_members:
if member in members_not_to_check:
Expand Down
35 changes: 35 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4809,3 +4809,38 @@ bad_rep(t) # E: Argument 1 to "bad_rep" has incompatible type "C"; expected "P[
# N: Got: \
# N: def rep(self) -> C
[builtins fixtures/tuple.pyi]

[case testProtocolInfiniteRecursionGrowingLeft]
# Regression test for https://github.com/python/mypy/issues/21739
# When the left type keeps growing but the right protocol stays the same,
# the assuming mechanism cannot detect the cycle. Guard against infinite recursion.
from dataclasses import dataclass
from typing import Any, Callable, Protocol

class PExp(Protocol):
def EQ(self, other: Any) -> "PExp": ...

class PRef(Protocol):
def EQ(self, other: Any) -> "PExp": ...

@dataclass
class Exp:
_left: Any
_op: str
_right: Any

def EQ(self, other: Any) -> "Exp":
return Exp(self, "EQ", other)

@dataclass
class Ref:
get: Callable[[], Any]

def EQ(self, other: Any) -> "Exp":
return Exp(self, "EQ", other)

@dataclass
class Cache:
def spam(self, name: str) -> "PRef":
return Ref(lambda: "spam")
[builtins fixtures/dataclasses.pyi]
Loading