← Back to index

generics_typevartuple_callable.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 0
FP: 3
FN: 1
Optional: 0 / 0
1"""
2Tests the use of TypeVarTuple within a Callable.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#type-variable-tuples-with-callable
6
7# > Type variable tuples can also be used in the arguments section of a Callable.
8
9
10from typing import Callable, TypeVar, TypeVarTuple, assert_type
12Ts = TypeVarTuple("Ts")
13T = TypeVar("T")
16class Process:
17 def __init__(self, target: Callable[[*Ts], None], args: tuple[*Ts]) -> None:
18 ...
21def func1(arg1: int, arg2: str) -> None:
22 ...
25Process(target=func1, args=(0, "")) # OK
26Process(target=func1, args=("", 0)) # E
Expected a ty diagnostic for this line
29def func2(f: Callable[[int, *Ts, T], tuple[T, *Ts]]) -> tuple[*Ts, T]:
30 raise NotImplementedError
33def callback1(a: int, b: str, c: int, d: complex) -> tuple[complex, str, int]:
34 raise NotImplementedError
37def callback2(a: int, d: str) -> tuple[str]:
38 raise NotImplementedError
41assert_type(func2(callback1), tuple[str, int, complex])
Unexpected error [type-assertion-failure] Type `tuple[@Todo(TypeVarTuple), ...]` does not match asserted type `tuple[str, int, int | float | complex]`
42assert_type(func2(callback2), tuple[str])
Unexpected error [type-assertion-failure] Type `tuple[@Todo(TypeVarTuple), ...]` does not match asserted type `tuple[str]`
45def func3(*args: *tuple[int, *Ts, T]) -> tuple[T, *Ts]:
46 raise NotImplementedError
49def has_int_and_str(a: int, b: str, c: float, d: complex):
50 assert_type(func3(a, b, c, d), tuple[complex, str, float])
Unexpected error [type-assertion-failure] Type `tuple[@Todo(TypeVarTuple), ...]` does not match asserted type `tuple[int | float | complex, str, int | float]`