If you have a value: Optional[X]
, a common way to check if it is None
is by by using the pattern:
from typing import Optional
def some_function(value: Optional[str]):
if not value:
print("oopsie! value is either None or the empty string")
return
... # now you know that value is not empty
The gotcha with this pattern is other values that evaluate to False
, e.g.:
X == str
:""
X == int
:0
X == float
:0.0
... interestingly,bool(float('nan')) is True
X == list
:[]
(similar for sets, tuples, dicts, or other iterables)
My question is: Does any datetime.datetime
object evaluate to False
? Any datetime.time
/ datetime.date
?