← Back to index
generics_typevartuple_unpack.py
True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 0
FP: 0
FN: 1
Optional: 0 / 0
1
"""
2
Tests
unpack
operations
for
TypeVarTuple
.
3
"""
4
5
# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#unpacking-tuple-types
6
7
from
typing
import
Any
,
Generic
,
NewType
,
TypeVarTuple
8
9
Height
=
NewType
(
"Height"
,
int
)
10
Width
=
NewType
(
"Width"
,
int
)
11
Batch
=
NewType
(
"Batch"
,
int
)
12
Channels
=
NewType
(
"Channels"
,
int
)
13
14
Ts
=
TypeVarTuple
(
"Ts"
)
15
16
17
class
Array
(
Generic
[
*
Ts
]):
18
...
19
20
21
def
process_batch_channels
(
x
:
Array
[
Batch
,
*
tuple
[
Any
,
...
],
Channels
])
->
None
:
22
...
23
24
25
def
func3
(
26
x
:
Array
[
Batch
,
Height
,
Width
,
Channels
],
y
:
Array
[
Batch
,
Channels
],
z
:
Array
[
Batch
]
27
):
28
process_batch_channels
(
x
)
# OK
29
process_batch_channels
(
y
)
# OK
30
process_batch_channels
(
z
)
# E
Expected a ty diagnostic for this line
31
32
33
Shape
=
TypeVarTuple
(
"Shape"
)
34
35
36
def
expect_variadic_array
(
x
:
Array
[
Batch
,
*
Shape
])
->
None
:
37
...
38
39
40
def
expect_precise_array
(
x
:
Array
[
Batch
,
Height
,
Width
,
Channels
])
->
None
:
41
...
42
43
44
def
func4
(
y
:
Array
[
*
tuple
[
Any
,
...
]]):
45
expect_variadic_array
(
y
)
# OK
46
expect_precise_array
(
y
)
# OK