← Back to index

overloads_basic.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 1
FP: 0
FN: 0
Optional: 0 / 0
1"""
2Tests the behavior of typing.overload.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/overload.html#overload
6
7from typing import (
8 Any,
9 Callable,
10 Iterable,
11 Iterator,
12 TypeVar,
13 assert_type,
14 overload,
15)
18class Bytes:
19 ...
21 @overload
22 def __getitem__(self, __i: int) -> int:
23 ...
25 @overload
26 def __getitem__(self, __s: slice) -> bytes:
27 ...
29 def __getitem__(self, __i_or_s: int | slice) -> int | bytes:
30 if isinstance(__i_or_s, int):
31 return 0
32 else:
33 return b""
36b = Bytes()
37assert_type(b[0], int)
38assert_type(b[0:1], bytes)
39b[""] # E: no matching overload
[invalid-argument-type] Method `__getitem__` of type `Overload[(__i: int, /) -> int, (__s: slice[Any, Any, Any], /) -> bytes]` cannot be called with key of type `Literal[""]` on object of type `Bytes`
42T1 = TypeVar("T1")
43T2 = TypeVar("T2")
44S = TypeVar("S")
47@overload
48def map(func: Callable[[T1], S], iter1: Iterable[T1]) -> Iterator[S]:
49 ...
52@overload
53def map(
54 func: Callable[[T1, T2], S], iter1: Iterable[T1], iter2: Iterable[T2]
55) -> Iterator[S]:
56 ...
59def map(func: Any, iter1: Any, iter2: Any = ...) -> Any:
60 pass