Registry / data / maybe-else

maybe-else

JSON →
library0.2.1pypypi✓ verified 22d ago

Provides a `Maybe` class as a Python implementation of null-aware operators. It allows for safe chaining of operations, attribute access, item access, and method calls on potentially `None` values, falling back to a default if `None` is encountered at any point. This helps avoid explicit `if var is not None` checks, similar to Optional/Maybe types in other languages. The library is currently at version 0.2.1, with its first and only release in November 2019.

pip install maybe-else
INSTALL
IMPORT
SIG · MAYBE-ELSE
M
maybe-else
datapythonv0.2.1
Install
1.5s avg
Import
11ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.008s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Maybe
from maybe import Maybe
from maybe_else import Maybe
maybe
from maybe import maybe
from maybe_else import maybe

The `Maybe` class wraps a value, allowing subsequent attribute, item, or method calls to be chained. If any step in the chain encounters a `None` or raises a caught exception, the entire chain short-circuits, and `else_()` returns the provided default. Otherwise, the result of the final operation is returned.

from maybe_else import Maybe # Basic usage: providing a default for None result1 = Maybe(None).else_("default value") print(f"Result 1: {result1}") # Chained operations on a non-None value data = {'user': {'name': 'Alice', 'age': 30}} maybe_name = Maybe(data)['user'].get('name').upper().else_('Unknown') print(f"Maybe name: {maybe_name}") # Chained operations on a path with None missing_data = {'user': None} maybe_age = Maybe(missing_data)['user'].get('age').else_(0) # 'user' is None, so default is used print(f"Maybe age (missing user): {maybe_age}") # Chained operations with a legitimate None as an intermediate result def get_nullable_value(x): return None if x % 2 == 0 else x result_none_output = Maybe(2).map(get_nullable_value).else_('Fallback') print(f"Result None output (even number): {result_none_output}") # Expected: None, not 'Fallback' (see warnings) result_non_none_output = Maybe(3).map(get_nullable_value).else_('Fallback') print(f"Result Non-None output (odd number): {result_non_none_output}")
Debug
Known issues
gotchaThe `Maybe` class only catches `IndexError`, `KeyError`, `AttributeError`, and `TypeError` during chained operations. Any other exception type raised within the chain (e.g., `ValueError`, `ZeroDivisionError`) will propagate normally and must be handled outside the `Maybe` construct.
fix
Be aware of the specific exceptions handled by `Maybe`. For other potential exceptions, use standard Python `try...except` blocks around the `Maybe` chain or ensure inputs prevent such errors.
affects: 0.2.1 and later
gotchaIf an intermediate operation in a `Maybe` chain *legitimately* returns `None` (i.e., `None` was not the initial wrapped value, nor did it result from a caught exception), `Maybe.else_()` will return `None` instead of the specified alternative value. This is because `None` is treated as a valid computed output.
fix
To differentiate between an initial `None` or an exception-induced `None` and a legitimate `None` result, you may need to check the final value explicitly after `else_()` or wrap functions that might return `None` more carefully if a 'true' fallback is always desired.
affects: 0.2.1 and later
gotchaThe truthiness of a `Maybe` object (`bool(Maybe(value))`) is determined by whether it would return its held `value` upon calling `else_()` (truthy) or the specified alternative (falsy). This might not always align with the truthiness of the underlying wrapped value.
fix
Always call `.else_()` to extract the actual value and handle its truthiness explicitly, rather than relying on the truthiness of the `Maybe` object itself, to avoid unexpected behavior.
affects: 0.2.1 and later
gotchaThe library's `requires_python` isn't specified on PyPI, but its GitHub indicates support for Python 3.7+. Using it with older Python versions might lead to compatibility issues. Python itself does not strictly follow Semantic Versioning, and minor version updates (e.g., 3.7 to 3.8) can introduce subtle breaking changes for certain libraries.
fix
Ensure your project uses Python 3.7 or newer. Always test your application thoroughly when upgrading Python minor versions, even with seemingly stable libraries.
affects: <0.2.1 and later, if used with Python <3.7
Upgrade
Version history
0.2.1latest on PyPI · released Nov 14, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
3 hits · last 30 days
node
2
Resources
maybe-else — pip install maybe-else · libregistry