@@ -185,7 +185,7 @@ class SeqError:
185185 def __contains__(self, x: int) -> bool:
186186 raise RuntimeError()
187187
188- def __len__(self):
188+ def __len__(self) -> int :
189189 return -5
190190
191191def any_seq_error() -> Any:
@@ -545,6 +545,7 @@ def test_type_mismatch_fall_back_to_reverse() -> None:
545545 assert F()**G() == -6
546546
547547[case testDundersBinaryNotImplemented]
548+ # mypy: allow-untyped-defs
548549from typing import Any, Union
549550from testutil import assertRaises
550551
@@ -617,15 +618,28 @@ def test_unannotated_add() -> None:
617618 with assertRaises(TypeError, "unsupported operand type(s) for +: 'F' and 'str'"):
618619 o + 'x'
619620
621+ o2: Any = F(4)
622+ assert o2 + 5 == 9
623+ with assertRaises(TypeError, "unsupported operand type(s) for +: 'F' and 'str'"):
624+ o2 + 'x'
625+
620626def test_unannotated_add_and_radd_1() -> None:
621627 o = F(4)
622628 assert o + G() == 5
623629
630+ o2: Any = F(4)
631+ assert o2 + G() == 5
632+
624633def test_unannotated_radd() -> None:
625634 assert 'x' + G() == 'a'
626635 with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'G'"):
627636 1 + G()
628637
638+ o: Any = G()
639+ assert 'x' + o == 'a'
640+ with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'G'"):
641+ 1 + o
642+
629643class H:
630644 def __add__(self, x):
631645 if isinstance(x, int):
@@ -644,40 +658,48 @@ def test_unannotated_add_and_radd_2() -> None:
644658 with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'H'"):
645659 1 + h
646660
661+ h2: Any = H()
662+ assert h + 5 == 6
663+ assert 'x' + h == 22
664+ with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'H'"):
665+ 1 + h
666+
647667# TODO: Inheritance
648668
649669[case testDifferentReverseDunders]
670+ from typing import Any
671+
650672class C:
651673 # __radd__ and __rsub__ are tested elsewhere
652674
653- def __rmul__(self, x) :
675+ def __rmul__(self, x: Any) -> int :
654676 return 1
655677
656- def __rtruediv__(self, x) :
678+ def __rtruediv__(self, x: Any) -> int :
657679 return 2
658680
659- def __rmod__(self, x) :
681+ def __rmod__(self, x: Any) -> int :
660682 return 3
661683
662- def __rfloordiv__(self, x) :
684+ def __rfloordiv__(self, x: Any) -> int :
663685 return 4
664686
665- def __rlshift__(self, x) :
687+ def __rlshift__(self, x: Any) -> int :
666688 return 5
667689
668- def __rrshift__(self, x) :
690+ def __rrshift__(self, x: Any) -> int :
669691 return 6
670692
671- def __rand__(self, x) :
693+ def __rand__(self, x: Any) -> int :
672694 return 7
673695
674- def __ror__(self, x) :
696+ def __ror__(self, x: Any) -> int :
675697 return 8
676698
677- def __rxor__(self, x) :
699+ def __rxor__(self, x: Any) -> int :
678700 return 9
679701
680- def __rmatmul__(self, x) :
702+ def __rmatmul__(self, x: Any) -> int :
681703 return 10
682704
683705def test_reverse_dunders() -> None:
@@ -803,10 +825,10 @@ def test_error() -> None:
803825 c += 'x'
804826
805827class BadInplaceAdd:
806- def __init__(self):
828+ def __init__(self) -> None :
807829 self.x = 0
808830
809- def __iadd__(self, x) :
831+ def __iadd__(self, x: int) -> Any :
810832 self.x += x
811833
812834def test_in_place_operator_returns_none() -> None:
0 commit comments