Option API Reference¶
API documentation for the Option type and related classes.
Type Alias¶
A type representing an optional value: either Some[T] (present) or Nothing (absent).
Some¶
Some
¶
Bases: Generic[T]
Some variant of Option containing a value.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value to wrap.
TYPE:
|
map_async
async
¶
map_async(
fn: Callable[[T], Coroutine[Any, Any, U]],
) -> Some[U]
Transform Some value with async function.
map_or_else
¶
Apply fn to the Some value.
and_then
¶
Chain Option-returning operation.
and_then_async
async
¶
and_then_async(
fn: Callable[
[T], Coroutine[Any, Any, Some[U] | _NothingType]
],
) -> Some[U] | _NothingType
Chain async Option-returning operation.
or_else
¶
Return self (has value, no need to recover).
or_else_async
async
¶
Return self (has value, no need to recover).
filter
¶
filter(
predicate: Callable[[T], bool],
) -> Some[T] | _NothingType
Return Some if predicate passes, else Nothing.
unwrap_or_else
¶
Return the Some value, ignoring the function.
unwrap_or_raise
¶
Return the Some value, ignoring the exception.
inspect_nothing
¶
inspect_nothing(fn: Callable[[], Any]) -> Some[T]
Return self (not Nothing, skip fn).
zip
¶
Zip with another Option.
zip_with
¶
Zip with another Option using a combining function.
xor
¶
Return Some if exactly one is Some, else Nothing.
Nothing (NOTHING)¶
The Nothing type represents the absence of a value. NOTHING is the singleton instance.
Methods¶
All methods on Nothing return appropriate "empty" values:
| Method | Return Value |
|---|---|
is_some() |
False |
is_nothing() |
True |
map(fn) |
NOTHING |
and_then(fn) |
NOTHING |
or_else(fn) |
Result of fn() |
filter(pred) |
NOTHING |
unwrap() |
Raises UnwrapError |
unwrap_or(default) |
default |
unwrap_or_else(fn) |
Result of fn() |
ok_or(err) |
Err(err) |
flatten() |
NOTHING |
zip(other) |
NOTHING |
xor(other) |
other if other is Some, else NOTHING |
to_tuple() |
() |
LazyOption¶
LazyOption
¶
LazyOption(
source: Awaitable[Some[T] | _NothingType]
| Some[T]
| _NothingType,
operations: tuple[OptionOperation, ...] = (),
)
Bases: Generic[T]
Lazy Option with deferred execution for clean async chaining.
LazyOption builds a pipeline of operations that execute only when
.collect() is called. All methods accept both sync and async
functions transparently.
Example
From an existing Option
from_option
classmethod
¶
from_option(
option: Some[T] | _NothingType,
) -> LazyOption[T]
Create LazyOption from an existing Option.
from_awaitable
classmethod
¶
from_awaitable(
awaitable: Awaitable[Some[T] | _NothingType],
) -> LazyOption[T]
Create LazyOption from a coroutine/awaitable that returns Option.
map
¶
map(
fn: Callable[[T], U | Awaitable[U]],
) -> LazyOption[U]
Transform Some value. fn can be sync or async.
and_then
¶
and_then(
fn: Callable[
[T],
Some[U]
| _NothingType
| Awaitable[Some[U] | _NothingType],
],
) -> LazyOption[U]
Chain Option-returning function. fn can be sync or async.
or_else
¶
or_else(
fn: Callable[
[],
Some[T]
| _NothingType
| Awaitable[Some[T] | _NothingType],
],
) -> LazyOption[T]
Recover from Nothing. fn can be sync or async.
filter
¶
filter(
predicate: Callable[[T], bool | Awaitable[bool]],
) -> LazyOption[T]
Filter based on predicate. predicate can be sync or async.
tee
¶
tee(fn: Callable[[T], Any]) -> LazyOption[T]
Side effect on Some value. fn can be sync or async.
inspect_nothing
¶
inspect_nothing(fn: Callable[[], Any]) -> LazyOption[T]
Side effect on Nothing. fn can be sync or async.
Type Guards¶
is_some¶
is_some
¶
Type guard to check if an option is Some.
Unlike the .is_some() method, this standalone function enables
proper type narrowing in conditional branches. This is necessary
because Python's type system doesn't support narrowing based on
method return types.
| PARAMETER | DESCRIPTION |
|---|---|
option
|
The Option to check.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeIs[Some[T]]
|
True if option is Some, with the type narrowed to Some[T]. |
is_nothing¶
is_nothing
¶
is_nothing(
option: Some[T] | _NothingType,
) -> TypeIs[_NothingType]
Type guard to check if an option is Nothing.
Unlike the .is_nothing() method, this standalone function enables
proper type narrowing in conditional branches. This is necessary
because Python's type system doesn't support narrowing based on
method return types.
| PARAMETER | DESCRIPTION |
|---|---|
option
|
The Option to check.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeIs[_NothingType]
|
True if option is Nothing, with the type narrowed to _NothingType. |
Utility Functions¶
from_nullable¶
from_nullable
¶
from_nullable(value: T | None) -> Some[T] | _NothingType
Convert a nullable value to Option.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A value that may be None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Some[T] | _NothingType
|
Some(value) if value is not None, otherwise Nothing. |
sequence_options¶
sequence_options
¶
Collect an iterable of Options into an Option of list.
Fails fast on the first Nothing encountered. If all Options are Some, returns Some containing a list of all values.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
Iterable of Option values to collect.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Some[list[T]] | _NothingType
|
Some(list) if all are Some, otherwise Nothing. |
traverse_options¶
traverse_options
¶
traverse_options(
items: Iterable[U],
fn: Callable[[U], Some[T] | _NothingType],
) -> Some[list[T]] | _NothingType
Map a function over items and collect Options.
Equivalent to sequence_options(map(fn, items)) but more efficient.
Fails fast on the first Nothing encountered.
| PARAMETER | DESCRIPTION |
|---|---|
items
|
Iterable of items to process.
TYPE:
|
fn
|
Function returning Option for each item.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Some[list[T]] | _NothingType
|
Some(list) if all succeed, otherwise Nothing. |