← 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
"""
2
Tests
the
behavior
of
typing
.
overload
.
3
"""
4
5
# Specification: https://typing.readthedocs.io/en/latest/spec/overload.html#overload
6
7
from
typing
import
(
8
Any
,
9
Callable
,
10
Iterable
,
11
Iterator
,
12
TypeVar
,
13
assert_type
,
14
overload
,
15
)
16
17
18
class
Bytes
:
19
...
20
21
@overload
22
def
__getitem__
(
self
,
__i
:
int
)
->
int
:
23
...
24
25
@overload
26
def
__getitem__
(
self
,
__s
:
slice
)
->
bytes
:
27
...
28
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
""
34
35
36
b
=
Bytes
()
37
assert_type
(
b
[
0
],
int
)
38
assert_type
(
b
[
0
:
1
],
bytes
)
39
b
[
""
]
# 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`
40
41
42
T1
=
TypeVar
(
"T1"
)
43
T2
=
TypeVar
(
"T2"
)
44
S
=
TypeVar
(
"S"
)
45
46
47
@overload
48
def
map
(
func
:
Callable
[[
T1
],
S
],
iter1
:
Iterable
[
T1
])
->
Iterator
[
S
]:
49
...
50
51
52
@overload
53
def
map
(
54
func
:
Callable
[[
T1
,
T2
],
S
],
iter1
:
Iterable
[
T1
],
iter2
:
Iterable
[
T2
]
55
)
->
Iterator
[
S
]:
56
...
57
58
59
def
map
(
func
:
Any
,
iter1
:
Any
,
iter2
:
Any
=
...
)
->
Any
:
60
pass
61