← 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"""
2Tests unpack operations for TypeVarTuple.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#unpacking-tuple-types
6
7from typing import Any, Generic, NewType, TypeVarTuple
8
9Height = NewType("Height", int)
10Width = NewType("Width", int)
11Batch = NewType("Batch", int)
12Channels = NewType("Channels", int)
14Ts = TypeVarTuple("Ts")
17class Array(Generic[*Ts]):
18 ...
21def process_batch_channels(x: Array[Batch, *tuple[Any, ...], Channels]) -> None:
22 ...
25def 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
33Shape = TypeVarTuple("Shape")
36def expect_variadic_array(x: Array[Batch, *Shape]) -> None:
37 ...
40def expect_precise_array(x: Array[Batch, Height, Width, Channels]) -> None:
41 ...
44def func4(y: Array[*tuple[Any, ...]]):
45 expect_variadic_array(y) # OK
46 expect_precise_array(y) # OK