If I have
def get(
ids: str | list[str] | int | list[int],
) -> float | list[float]:
Is there a way to specify in return value annotation that a list of float
s is output only when the input ids
is a list of str
s or int
s?
One way to do this is by using the @overload
decorator to create 2 extra function signatures, one for int | str
that returns float
and one for list[str] | list[int]
that returns list[float]
and then have the actual function definition like you have now.
from typing import overload
@overload
def test(
ids:str | int,
) -> float:...
@overload
def test(
ids:list[str] | list[int],
) -> list[float]:...
def test(
ids: str | list[str] | int | list[int],
) -> float | list[float]:...