Python 3.5+ allows optional type hints. To type hint a union (i.e. x or y), use the Union type:
from typing import Union
def hello_or_nothing(msg: str) -> Union[str, None]:
if msg == "hello":
return "hello"
else:
return None
See the documentation.
Note: there is an explicit Optional type which is equivalent to Union[x, None]
.