@@ -185,7 +185,7 @@ class SeqError:
185
185
def __contains__(self, x: int) -> bool:
186
186
raise RuntimeError()
187
187
188
- def __len__(self):
188
+ def __len__(self) -> int :
189
189
return -5
190
190
191
191
def any_seq_error() -> Any:
@@ -545,6 +545,7 @@ def test_type_mismatch_fall_back_to_reverse() -> None:
545
545
assert F()**G() == -6
546
546
547
547
[case testDundersBinaryNotImplemented]
548
+ # mypy: allow-untyped-defs
548
549
from typing import Any, Union
549
550
from testutil import assertRaises
550
551
@@ -617,15 +618,28 @@ def test_unannotated_add() -> None:
617
618
with assertRaises(TypeError, "unsupported operand type(s) for +: 'F' and 'str'"):
618
619
o + 'x'
619
620
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
+
620
626
def test_unannotated_add_and_radd_1() -> None:
621
627
o = F(4)
622
628
assert o + G() == 5
623
629
630
+ o2: Any = F(4)
631
+ assert o2 + G() == 5
632
+
624
633
def test_unannotated_radd() -> None:
625
634
assert 'x' + G() == 'a'
626
635
with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'G'"):
627
636
1 + G()
628
637
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
+
629
643
class H:
630
644
def __add__(self, x):
631
645
if isinstance(x, int):
@@ -644,40 +658,48 @@ def test_unannotated_add_and_radd_2() -> None:
644
658
with assertRaises(TypeError, "unsupported operand type(s) for +: 'int' and 'H'"):
645
659
1 + h
646
660
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
+
647
667
# TODO: Inheritance
648
668
649
669
[case testDifferentReverseDunders]
670
+ from typing import Any
671
+
650
672
class C:
651
673
# __radd__ and __rsub__ are tested elsewhere
652
674
653
- def __rmul__(self, x) :
675
+ def __rmul__(self, x: Any) -> int :
654
676
return 1
655
677
656
- def __rtruediv__(self, x) :
678
+ def __rtruediv__(self, x: Any) -> int :
657
679
return 2
658
680
659
- def __rmod__(self, x) :
681
+ def __rmod__(self, x: Any) -> int :
660
682
return 3
661
683
662
- def __rfloordiv__(self, x) :
684
+ def __rfloordiv__(self, x: Any) -> int :
663
685
return 4
664
686
665
- def __rlshift__(self, x) :
687
+ def __rlshift__(self, x: Any) -> int :
666
688
return 5
667
689
668
- def __rrshift__(self, x) :
690
+ def __rrshift__(self, x: Any) -> int :
669
691
return 6
670
692
671
- def __rand__(self, x) :
693
+ def __rand__(self, x: Any) -> int :
672
694
return 7
673
695
674
- def __ror__(self, x) :
696
+ def __ror__(self, x: Any) -> int :
675
697
return 8
676
698
677
- def __rxor__(self, x) :
699
+ def __rxor__(self, x: Any) -> int :
678
700
return 9
679
701
680
- def __rmatmul__(self, x) :
702
+ def __rmatmul__(self, x: Any) -> int :
681
703
return 10
682
704
683
705
def test_reverse_dunders() -> None:
@@ -803,10 +825,10 @@ def test_error() -> None:
803
825
c += 'x'
804
826
805
827
class BadInplaceAdd:
806
- def __init__(self):
828
+ def __init__(self) -> None :
807
829
self.x = 0
808
830
809
- def __iadd__(self, x) :
831
+ def __iadd__(self, x: int) -> Any :
810
832
self.x += x
811
833
812
834
def test_in_place_operator_returns_none() -> None:
0 commit comments