@@ -6887,3 +6887,59 @@ class A:
68876887 def i(self) -> Union[bool, NotImplementedType]: return NotImplemented
68886888 def j(self) -> Union[bool, NotImplementedType]: return True
68896889[builtins fixtures/notimplemented.pyi]
6890+
6891+ [case testNotImplementedReturnedFromBinaryMagicMethod]
6892+ # flags: --warn-unreachable
6893+ from typing import Union
6894+
6895+ class A:
6896+ def __add__(self, x: A) -> Union[int, NotImplementedType]: ...
6897+ def __sub__(self, x: A) -> NotImplementedType: ...
6898+ def __imul__(self, x: A) -> Union[A, NotImplementedType]: ...
6899+ def __itruediv__(self, x: A) -> Union[A, NotImplementedType]: ...
6900+ def __ifloordiv__(self, x: A) -> Union[int, NotImplementedType]: ...
6901+ def __eq__(self, x: object) -> Union[bool, NotImplementedType]: ...
6902+ def __le__(self, x: int) -> Union[bool, NotImplementedType]: ...
6903+ def __lt__(self, x: int) -> NotImplementedType: ...
6904+ def __and__(self, x: object) -> NotImplementedType: ...
6905+ class B(A):
6906+ def __radd__(self, x: A) -> Union[int, NotImplementedType]: ...
6907+ def __rsub__(self, x: A) -> NotImplementedType: ...
6908+ def __itruediv__(self, x: A) -> Union[A, NotImplementedType]: ...
6909+ def __ror__(self, x: object) -> NotImplementedType: ...
6910+
6911+ a: A
6912+ b: B
6913+
6914+ reveal_type(a.__add__(a)) # N: Revealed type is "Union[builtins.int, builtins._NotImplementedType]"
6915+ reveal_type(a.__sub__(a)) # N: Revealed type is "builtins._NotImplementedType"
6916+ reveal_type(a.__imul__(a)) # N: Revealed type is "Union[__main__.A, builtins._NotImplementedType]"
6917+ reveal_type(a.__eq__(a)) # N: Revealed type is "Union[builtins.bool, builtins._NotImplementedType]"
6918+ reveal_type(a.__le__(1)) # N: Revealed type is "Union[builtins.bool, builtins._NotImplementedType]"
6919+
6920+ reveal_type(a + a) # N: Revealed type is "builtins.int"
6921+ reveal_type(a - a) # N: Revealed type is "Any"
6922+ reveal_type(a + b) # N: Revealed type is "builtins.int"
6923+ reveal_type(a - b) # N: Revealed type is "Any"
6924+ def f1(a: A) -> None:
6925+ a += a # E: Incompatible types in assignment (expression has type "int", variable has type "A")
6926+ def f2(a: A) -> None:
6927+ a -= a
6928+ reveal_type(a) # N: Revealed type is "__main__.A"
6929+ def f3(a: A) -> None:
6930+ a *= a
6931+ reveal_type(a) # N: Revealed type is "__main__.A"
6932+ def f4(a: A) -> None:
6933+ a /= a
6934+ reveal_type(a) # N: Revealed type is "__main__.A"
6935+ def f5(a: A) -> None:
6936+ a //= a # E: Result type of // incompatible in assignment
6937+ reveal_type(a == a) # N: Revealed type is "builtins.bool"
6938+ reveal_type(a == 1) # N: Revealed type is "builtins.bool"
6939+ reveal_type(a <= 1) # N: Revealed type is "builtins.bool"
6940+ reveal_type(a < 1) # N: Revealed type is "Any"
6941+ reveal_type(a and int()) # N: Revealed type is "Union[__main__.A, builtins.int]"
6942+ reveal_type(int() or a) # N: Revealed type is "Union[builtins.int, __main__.A]"
6943+
6944+ [builtins fixtures/notimplemented.pyi]
6945+
0 commit comments