@@ -734,17 +734,56 @@ def check(x: _T, y: _T) -> bool:
734734[case testReversibleOpOnTypeVarProtocol]
735735# https://github.com/python/mypy/issues/18203
736736from typing import Protocol, TypeVar, Union
737- from typing_extensions import Self
737+ from typing_extensions import Self, runtime_checkable 
738738
739739class A(Protocol):
740740    def __add__(self, other: Union[int, Self]) -> Self: ...
741741    def __radd__(self, other: Union[int, Self]) -> Self: ...
742742
743743AT = TypeVar("AT", bound=Union[int, A])
744744
745- def f(a: AT, _b: AT) -> None:
746-     a + a
747- [builtins fixtures/ops.pyi]
745+ def f(a: AT, b: AT) -> None:
746+     reveal_type(a + a)  # N: Revealed type is "Union[builtins.int, AT`-1]"
747+     reveal_type(a + b)  # N: Revealed type is "Union[builtins.int, AT`-1]"
748+     if isinstance(a, int):
749+         reveal_type(a)  # N: Revealed type is "AT`-1"
750+         reveal_type(a + a)  # N: Revealed type is "builtins.int"
751+         reveal_type(a + b)  # N: Revealed type is "Union[builtins.int, AT`-1]"
752+         reveal_type(b + a)  # N: Revealed type is "Union[builtins.int, AT`-1]"
753+ 
754+ @runtime_checkable
755+ class B(Protocol):
756+     def __radd__(self, other: Union[int, Self]) -> Self: ...
757+ 
758+ BT = TypeVar("BT", bound=Union[int, B])
759+ 
760+ def g(a: BT, b: BT) -> None:
761+     reveal_type(a + a)  # E: Unsupported left operand type for + ("BT") \
762+                         # N: Both left and right operands are unions \
763+                         # N: Revealed type is "Union[builtins.int, BT`-1, Any]"
764+     reveal_type(a + b)  # E: Unsupported left operand type for + ("BT") \
765+                         # N: Both left and right operands are unions \
766+                         # N: Revealed type is "Union[builtins.int, BT`-1, Any]"
767+     if isinstance(a, int):
768+         reveal_type(a)  # N: Revealed type is "BT`-1"
769+         reveal_type(0 + a)  # N: Revealed type is "builtins.int"
770+         reveal_type(a + 0)  # N: Revealed type is "builtins.int"
771+         reveal_type(a + a)  # N: Revealed type is "builtins.int"
772+         reveal_type(a + b)  # N: Revealed type is "Union[builtins.int, BT`-1]"
773+         reveal_type(b + a)  # E: Unsupported left operand type for + ("BT") \
774+                             # N: Left operand is of type "BT" \
775+                             # N: Revealed type is "Union[builtins.int, Any]"
776+     if isinstance(a, B):
777+         reveal_type(a)  # N: Revealed type is "BT`-1"
778+         reveal_type(0 + a)  # N: Revealed type is "BT`-1"
779+         reveal_type(a + 0)  # E: Unsupported left operand type for + ("BT") \
780+                             # N: Revealed type is "Any"
781+         reveal_type(a + a)  # E: Unsupported left operand type for + ("BT") \
782+                             # N: Revealed type is "Any"
783+         reveal_type(a + b)  # E: Unsupported left operand type for + ("BT") \
784+                             # N: Right operand is of type "BT" \
785+                             # N: Revealed type is "Any"
786+ [builtins fixtures/isinstance.pyi]
748787
749788
750789[case testErrorContextAndBinaryOperators]
0 commit comments