We want to type hint a function
def f(*args:float)->tuple[float,...]:
...
return tuple(args)
such that it is specified that the number of elements in the tuple matches the number of args. Of course, the return here is a placeholder for more complicated logic.
We would like to use mypy or pylance to check if we always return a) the correct number of elements and b) the correct tyoe of all elements.
Using TypeVarTuple (https://peps.python.org/pep-0646/#args-as-a-type-variable-tuple) would allow to specify that we return the same number of elements, but not the type.
Is there in current python (3.12) way to do it besides writing many overloads for 1-parameter, 2-parameter, 3-parameters etc?
Yes, you can write a no-op decorator to make the signature of
freject attempts at passing a type that you don't want. The following example makesfreject any attempts at passing values which aren't compatible withfloat.Demo: mypy Playground, Pyright Playground