Is it possible to add type hint to a reloaded python function?
i.e. function’s return type is determined by the type of one or more of it’s input arguments.
For example:
from torch import Tensor
from numpy import ndarry
# The function I want to have "type reloading"
def fun(x: Tensor | ndarry) -> Tensor | ndarry:
if type(x) is Tensor:
# Process torch tensor and return a Tensor
pass
elif type(x) is ndarry:
# Process numpy matrix and return an ndarry
pass
Demonstration of what I wanted (will NOT work as is):
# Reload 1
def fun(x: Tensor) -> Tensor:
# Do something and return a Tensor
pass
# Reload 2
def fun(x: ndarry) -> ndarry:
# Do something and return an ndarry
pass
New contributor
Yuxuan Zhang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1