How do I type hint an optional output parameter:
def myfunc(
x: float,
return_y: bool = False
) -> float, Optional[float] : # !!! WRONG !!!
# ... code here
#
# y and z are floats
if return_y:
return z, y
return z
--- edit
-> Tuple[float, Union[None, float]] :
but that is so ugly and seems to overwhelm the fact that typically it will only return a simple float. Is that the correct way to do this?
See answer below for correct way.
Note: this design is not a good practice and should be avoided in favour of a consistent return type. Still, in case it has to be done, by optional output parameter it is meant a parameter that may not be returned depending on an input argument's flag.