I'm a big fan of functional programming but this documentation is kind of funny, unintentionally. I realize there is no better way to do it in Python.
And that’s how your initial refactored code will look like:
user: Optional[User]
can_buy_stuff: Maybe[bool] = Maybe.from_value(user).map( # type hint is not required
lambda real_user: real_user.get_balance(),
).map(
lambda balance: balance.credit_amount(),
).map(
lambda balance_credit: balance_credit > 0,
)
Much better, isn’t it?