Skip to content
Merged
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
12 changes: 12 additions & 0 deletions mypy/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ def pre_validate_solutions(
"""
new_solutions: list[Type | None] = []
for t, s in zip(original_vars, solutions):
if is_callable_protocol(t.upper_bound):
# This is really ad-hoc, but a proper fix would be much more complex,
# and otherwise this may cause crash in a relatively common scenario.
new_solutions.append(s)
continue
if s is not None and not is_subtype(s, t.upper_bound):
bound_satisfies_all = True
for c in constraints:
Expand All @@ -567,3 +572,10 @@ def pre_validate_solutions(
continue
new_solutions.append(s)
return new_solutions


def is_callable_protocol(t: Type) -> bool:
proper_t = get_proper_type(t)
if isinstance(proper_t, Instance) and proper_t.type.is_protocol:
return "__call__" in proper_t.type.protocol_members
return False
28 changes: 28 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -2132,3 +2132,31 @@ class D:
x: int
x: Union[C, D]
reveal_type(x.x) # N: Revealed type is "Union[__main__.C, builtins.int]"

[case testCallableProtocolTypingSelf]
from typing import Protocol, Self

class MyProtocol(Protocol):
__name__: str

def __call__(
self: Self,
) -> None: ...

def test() -> None: ...
value: MyProtocol = test

[case testCallableProtocolOldSelf]
from typing import Protocol, TypeVar

Self = TypeVar("Self", bound="MyProtocol")

class MyProtocol(Protocol):
__name__: str

def __call__(
self: Self,
) -> None: ...

def test() -> None: ...
value: MyProtocol = test