TypeVarTuple
and Cartesian Product of functions
#1545
-
Consider the Cartesian product of functions def prod_fn(*funcs):
r"""Cartesian Product of Functions.
It is assumed every function takes a single positional argument.
"""
def __prod_fn(args, /):
"""Argument is a tuple with the input for each function."""
return tuple(f(arg) for f, arg in zip(funcs, args))
return __prod_fn How to type hint it? It should be something along the lines of def prod_fn(*funcs: *tuple[???]) -> Callable[[tuple[*U]], tuple[*V]]:
r"""Cartesian Product of Functions.
It is assumed every function takes a single positional argument.
"""
def __prod_fn(args: tuple[*U], /) -> tuple[*V]:
"""Argument is a tuple with the input for each function."""
return tuple(f(arg) for f, arg in zip(funcs, args))
return __prod_fn Where |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This just isn't possible yet, this would require something like a Right now you can only unpack a P.S. Wouldn't you need to use varargs for it to actually be the cartesian product of functions? The definition to me looks like the resulting function is supposed to have as many arguments as input functions, not one argument that's a tuple of all the inputs. def prod_fn(*funcs: *Map[(U, V), Callable[[U], V]]]) -> Callable[[*U], tuple[*V]]:
r"""Cartesian Product of Functions.
It is assumed every function takes a single positional argument.
"""
def __prod_fn(*args: *U) -> tuple[*V]:
"""Argument is a tuple with the input for each function."""
return tuple(f(arg) for f, arg in zip(funcs, args))
return __prod_fn |
Beta Was this translation helpful? Give feedback.
This just isn't possible yet, this would require something like a
Map
construct to apply a type to each element of theTypeVarTuple
, (although this case is even more complex since there's twoTypeVarTuple
that both need to be mapped at the same time)Right now you can only unpack a
TypeVarTuple
directly into other variadic generics (i.e.tuple
,Callable
,Union
and any user-defined generics that take aTypeVarTuple
).P.S. Wouldn't you need to use varargs for it to actually be the cartesian product of functions? The definition to me looks like the resulting function is supposed to have as many arguments as input functions, not one argument that's a tuple of all the inputs.