Skip to content
Closed
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
7 changes: 6 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
TypeAliasType,
TypedDictType,
TypeOfAny,
TypeStrVisitor,
TypeType,
TypeVarLikeType,
TypeVarTupleType,
Expand Down Expand Up @@ -1310,7 +1311,11 @@ def f(self) -> A: ...
return False
assuming = right.type.assuming_proper if proper_subtype else right.type.assuming
if len(assuming) > MAX_PROTOCOL_DEPTH:
return True
visitor = TypeStrVisitor(options=options or Options())
pairs = []
for l, r in assuming:
pairs.append((l.accept(visitor), r.accept(visitor)))
raise ValueError(pairs)
for l, r in reversed(assuming):
if l == left and r == right:
return True
Expand Down
2 changes: 1 addition & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class SentinelValue(NamedTuple):
# Note we set very large cut-off for now, since some numerical libraries currently
# use ~25 assumptions, and we want to avoid false negatives with them. We may be able
# to reduce this in the future if we will optimize our callable unification logic.
MAX_PROTOCOL_DEPTH: Final = 40
MAX_PROTOCOL_DEPTH: Final = 11


class TypeOfAny:
Expand Down
62 changes: 61 additions & 1 deletion test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4790,7 +4790,7 @@ bad_rep(t) # E: Argument 1 to "bad_rep" has incompatible type "C"; expected "P[
# N: def rep(self) -> C
[builtins fixtures/tuple.pyi]

[case testDivergingProtocol]
[case testDivergingProtocol-skip]
from typing import Protocol, TypeVar, List

T = TypeVar("T")
Expand All @@ -4803,3 +4803,63 @@ class C:
# This should not cause infinite recursion.
x: P[int] = C()
[builtins fixtures/tuple.pyi]

[case testProtocolLongOverload-skip]
from typing import Protocol, Generic, TypeVar, overload

class P(Protocol):
def foo(self, x: int) -> P: ...

class A1: ...
class A2: ...
class A3: ...
class A4: ...
class A5: ...
class A6: ...
class A7: ...
class A8: ...
class A9: ...
class A10: ...
class A11: ...
class A12: ...
class A13: ...
class A14: ...
class A15: ...

T = TypeVar("T")
class C(Generic[T]):
@overload
def foo(self, x: A1) -> C[A1]: ...
@overload
def foo(self, x: A2) -> C[A2]: ...
@overload
def foo(self, x: A3) -> C[A3]: ...
@overload
def foo(self, x: A4) -> C[A4]: ...
@overload
def foo(self, x: A5) -> C[A5]: ...
@overload
def foo(self, x: A6) -> C[A6]: ...
@overload
def foo(self, x: A7) -> C[A7]: ...
@overload
def foo(self, x: A8) -> C[A8]: ...
@overload
def foo(self, x: A9) -> C[A9]: ...
@overload
def foo(self, x: A10) -> C[A10]: ...
@overload
def foo(self, x: A11) -> C[A11]: ...
@overload
def foo(self, x: A12) -> C[A12]: ...
@overload
def foo(self, x: A13) -> C[A13]: ...
@overload
def foo(self, x: A14) -> C[A14]: ...
@overload
def foo(self, x: A15) -> C[A15]: ...
def foo(self, x):
pass

x: P = C()
[builtins fixtures/tuple.pyi]
Loading