Add runtime support for optional types
While working on -strict-optional mode in mypy, I noticed patterns like this many times:
def func(x: Optional[X]) -> Optional[Y]: if x is None: return None # do some stuff with 'x'
The most recent example appeared in python/mypy#3295. I could imagine this pattern (monadic behaviour) is quite common. If one uses "error as value" (i.e. passing None instead of using an exception), then the above pattern is necessary for "piping". Maybe we could provide a decorator that will automatically add such behaviour to a function, schematically:
def maybe(func): def inner(*args): if None in args: return None return func(*args) return inner @maybe def convert_to_str(x): return 'converted: ' + str(x)
The decorator is very simple, but the thing it does looks very common.