I wrote a small utility function:
def maybe_apply(x: T | None, f: Callable[[T], Y]) -> Y | None:
if x is not None:
return f(x)
Unlike decorator solution it works with library functions as well and is more explicit. Also it deduces types.