Kinda nice (and not ugly) optional chaining for Python. Access nested attributes without crashing on Nones.
from optiver import opt
user = get_user() # .profile might be None
theme = ~opt(user).profile.settings.theme # theme is str | None, won't crashopt(value) # wrap a value
~wrapped # unwrap to get resultChain any attributes. If any value in the chain is None, the rest short-circuits and returns None. Missing attrs still raise AttributeError.
pip install optiver # TODO: publish to PyPI~is the unwrap operator (Python's__invert__)- Crashes on missing attrs (like normal Python)
- Safely chains through
Nonevalues - No runtime dependencies
- Unfortunately kinda hard to type-check this but if you really cared about that would you really be using Python. Just add unit tests!