Goal is to stick to fully static typings. What is the best way to "resolve" a Union/Optional type and satisfy mypy? For Example:
from typing import Optional
def foo() -> bool:
item: Optional[str] = find() # here find is some arbitrary search which returns a string in case something was found or None if nothing found.
if(item == None):
return False # Nothing found in the first place, could not execute bar()
else:
return bar(item) # here mypy is unsatisfied and tells me, that type Optional[str] is not applicable to type str.
def bar(str) -> bool:
.....
So what would be the best way to "cast" the Optional[str] to str? Is tehre a specific pythonian way to do that?