PyMaybe
raw JSON → 0.1.6 verified Fri May 01 auth: no python
pymaybe implements Haskell-style Maybe monad in Python, allowing safe chaining of operations on values that may be None. Version 0.1.6 is the latest; releases are infrequent and no breaking changes are documented. The library wraps values in a Maybe container and provides a .maybe attribute for safe access.
pip install pymaybe Common errors
error AttributeError: module 'pymaybe' has no attribute 'Maybe' ↓
cause Direct import of the module and attempting to use Maybe class directly.
fix
Use
from pymaybe import maybe and then maybe(value). error TypeError: 'Maybe' object is not subscriptable ↓
cause Trying to use bracket indexing on a Maybe object, e.g., maybe_dict['key'].
fix
Use
.get('key') method instead of bracket syntax. Warnings
gotcha Always call .maybe at the end of a chain to unwrap the value; forgetting it returns a Maybe object, causing subtle bugs. ↓
fix Append .maybe to the final expression, e.g., result = maybe(x).get('key').maybe
gotcha The .maybe attribute on a Maybe(None) returns None, but the Maybe object itself is truthy. Use 'value.maybe is None' to check for absence. ↓
fix Use `if result.maybe is None:` instead of `if not result:`
gotcha Arithmetic and comparison operators on Maybe objects behave differently: maybe(5) + maybe(3) returns Maybe(8), but comparing with == checks the wrapped value, not identity. ↓
fix Be aware that operators work on the inner value. For identity checks, unwrap first.
Imports
- maybe wrong
import pymaybecorrectfrom pymaybe import maybe
Quickstart
from pymaybe import maybe
# Wrap a potentially None value
data = maybe({'name': 'Alice', 'job': None})
# Safe access using .maybe (returns maybe(None) if missing)
job = data.get('job').maybe
print(job) # None
# Chaining: get nested key safely
address = data.get('address').maybe.get('city').maybe
print(address) # None