@@ -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
@@ -595,16 +596,16 @@ def test_any_radd() -> None:
595596 assert d3 + e3 == 4
596597
597598class F:
598- def __init__(self, v: Any ):
599+ def __init__(self, v):
599600 self.v = v
600601
601- def __add__(self, x: Any) -> Any :
602+ def __add__(self, x) :
602603 if isinstance(x, int):
603604 return self.v + x
604605 return NotImplemented
605606
606607class G:
607- def __radd__(self, x: Any) -> Any :
608+ def __radd__(self, x) :
608609 if isinstance(x, F):
609610 return x.v + 1
610611 if isinstance(x, str):
@@ -617,22 +618,35 @@ 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:
630- def __add__(self, x: Any) -> Any :
644+ def __add__(self, x) :
631645 if isinstance(x, int):
632646 return x + 1
633647 return NotImplemented
634648
635- def __radd__(self, x: Any) -> Any :
649+ def __radd__(self, x) :
636650 if isinstance(x, str):
637651 return 22
638652 return NotImplemented
@@ -644,6 +658,12 @@ 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]
0 commit comments