Skip to content

Commit 7e446b4

Browse files
authored
[mypyc] Add type annotations to tests (#19794)
Missing type annotations can compromise test coverage. My eventual goal is to require annotations by default in all run tests.
1 parent d33c147 commit 7e446b4

File tree

4 files changed

+106
-60
lines changed

4 files changed

+106
-60
lines changed

mypyc/test-data/fixtures/ir.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def __add__(self, value: List[_S], /) -> List[_S | _T]: ...
243243
def __iadd__(self, value: Iterable[_T], /) -> List[_T]: ... # type: ignore[misc]
244244
def append(self, x: _T) -> None: pass
245245
def pop(self, i: int = -1) -> _T: pass
246-
def count(self, _T) -> int: pass
246+
def count(self, x: _T) -> int: pass
247247
def extend(self, l: Iterable[_T]) -> None: pass
248248
def insert(self, i: int, x: _T) -> None: pass
249249
def sort(self) -> None: pass
@@ -365,7 +365,7 @@ def reversed(object: Sequence[_T]) -> Iterator[_T]: ...
365365
def id(o: object) -> int: pass
366366
# This type is obviously wrong but the test stubs don't have Sized anymore
367367
def len(o: object) -> int: pass
368-
def print(*object) -> None: pass
368+
def print(*args: object) -> None: pass
369369
def isinstance(x: object, t: object) -> bool: pass
370370
def iter(i: Iterable[_T]) -> Iterator[_T]: pass
371371
@overload

mypyc/test-data/fixtures/typing-full.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ from abc import abstractmethod, ABCMeta
1111
class GenericMeta(type): pass
1212

1313
class _SpecialForm:
14-
def __getitem__(self, index): ...
14+
def __getitem__(self, index: Any) -> Any: ...
1515
class TypeVar:
16-
def __init__(self, name, *args, bound=None): ...
17-
def __or__(self, other): ...
16+
def __init__(self, name: str, *args: Any, bound: Any = None): ...
17+
def __or__(self, other: Any) -> Any: ...
1818

1919
cast = 0
2020
overload = 0

mypyc/test-data/run-dunders.test

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

191191
def 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
548549
from typing import Any, Union
549550
from 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+
620626
def 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+
624633
def 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+
629643
class 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+
650672
class 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

683705
def test_reverse_dunders() -> None:
@@ -803,10 +825,10 @@ def test_error() -> None:
803825
c += 'x'
804826

805827
class 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

812834
def test_in_place_operator_returns_none() -> None:

0 commit comments

Comments
 (0)