I wonder what's the right way to type annotate a python function as below?
def f(value):
if isinstance(value, str):
return None
return value
This is a toy example that is analogous to a python I encountered that needs type annotated. Using Union like
def f(value: Union[float, int, str]) -> Union[float, int, None]:
if isinstance(value, str):
return None
return value
doesn't feel right because it doesn't enforce the rules
- an
intinput must result in anintoutput. - a
floatinput must result in afloatoutput. - a
strinput must result inNone.
Let's assume the input can only be one of int/float/str.