File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -553,6 +553,11 @@ def pre_validate_solutions(
553
553
"""
554
554
new_solutions : list [Type | None ] = []
555
555
for t , s in zip (original_vars , solutions ):
556
+ if is_callable_protocol (t .upper_bound ):
557
+ # This is really ad-hoc, but a proper fix would be much more complex,
558
+ # and otherwise this may cause crash in a relatively common scenario.
559
+ new_solutions .append (s )
560
+ continue
556
561
if s is not None and not is_subtype (s , t .upper_bound ):
557
562
bound_satisfies_all = True
558
563
for c in constraints :
@@ -567,3 +572,10 @@ def pre_validate_solutions(
567
572
continue
568
573
new_solutions .append (s )
569
574
return new_solutions
575
+
576
+
577
+ def is_callable_protocol (t : Type ) -> bool :
578
+ proper_t = get_proper_type (t )
579
+ if isinstance (proper_t , Instance ) and proper_t .type .is_protocol :
580
+ return "__call__" in proper_t .type .protocol_members
581
+ return False
Original file line number Diff line number Diff line change @@ -2132,3 +2132,31 @@ class D:
2132
2132
x: int
2133
2133
x: Union[C, D]
2134
2134
reveal_type(x.x) # N: Revealed type is "Union[__main__.C, builtins.int]"
2135
+
2136
+ [case testCallableProtocolTypingSelf]
2137
+ from typing import Protocol, Self
2138
+
2139
+ class MyProtocol(Protocol):
2140
+ __name__: str
2141
+
2142
+ def __call__(
2143
+ self: Self,
2144
+ ) -> None: ...
2145
+
2146
+ def test() -> None: ...
2147
+ value: MyProtocol = test
2148
+
2149
+ [case testCallableProtocolOldSelf]
2150
+ from typing import Protocol, TypeVar
2151
+
2152
+ Self = TypeVar("Self", bound="MyProtocol")
2153
+
2154
+ class MyProtocol(Protocol):
2155
+ __name__: str
2156
+
2157
+ def __call__(
2158
+ self: Self,
2159
+ ) -> None: ...
2160
+
2161
+ def test() -> None: ...
2162
+ value: MyProtocol = test
You can’t perform that action at this time.
0 commit comments