Option and Result
Option[T] represents a present or absent value. Result[T] contains either a
successful value or an exception.
Option
fpstreams.Option
dataclass
Bases: Generic[T]
Store either one non-None value or an empty state.
Instances are immutable and cannot distinguish a present None from absence. Use
of(), of_nullable(),
or empty() to make that choice explicit.
of
classmethod
Create a present option from a non-None value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
Value to store. |
required |
Returns:
| Type | Description |
|---|---|
Option[T]
|
A present option containing |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
of_nullable
classmethod
Create a present option from value, or an empty option from None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T | None
|
Nullable value to convert. |
required |
Returns:
| Type | Description |
|---|---|
Option[T]
|
|
empty
classmethod
is_present
Report whether this option stores a non-None value.
Returns:
| Type | Description |
|---|---|
bool
|
|
is_empty
Report whether this option stores None as its empty marker.
Returns:
| Type | Description |
|---|---|
bool
|
|
if_present
Invoke action once with the stored value, if one is present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Callable[[T], None]
|
Side-effecting callable skipped for an empty option. |
required |
filter
Keep a present value only when predicate accepts it.
Empty options and accepted values return the current immutable instance. A rejected
value produces a new empty option. Exceptions from predicate propagate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
Callable evaluated only for a present value. |
required |
Returns:
| Type | Description |
|---|---|
Option[T]
|
This option when empty or accepted; otherwise an empty option. |
map
Map a present value and convert a mapped None to absence.
The mapper is skipped for an empty option. Unlike Result, this method does not capture mapper exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
Callable[[T], R | None]
|
Callable applied to the stored value. |
required |
Returns:
| Type | Description |
|---|---|
Option[R]
|
An option containing the mapped value, or an empty option if this option is
empty or the mapper returns |
flat_map
Return the option produced by mapping a present value.
The mapper is skipped for an empty option. Its return value and any exception are passed through without additional wrapping or validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
Callable[[T], Option[R]]
|
Option-returning callable applied to the stored value. |
required |
Returns:
| Type | Description |
|---|---|
Option[R]
|
The mapper's result, or an empty option when this option is empty. |
unwrap
Extract the stored value, rejecting an empty option.
Returns:
| Type | Description |
|---|---|
T
|
The contained value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If this option is empty. |
or_else
Return the stored value, or the eagerly supplied fallback when empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
T
|
Value to return only when this option is empty. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The contained value, or |
or_else_get
Return the stored value, or lazily call supplier when empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
supplier
|
Callable[[], T]
|
Zero-argument fallback callable, skipped for a present value. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The contained value, or the value supplied when this option is empty. |
or_else_throw
Return the stored value, or construct and raise an exception when empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
Callable[[], Exception]
|
A zero-argument callable that creates the exception to raise. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The contained value. |
Raises:
| Type | Description |
|---|---|
Exception
|
The exception produced by |
Result
fpstreams.Result
Bases: Generic[T]
Common interface for a successful Ok or failed Err value.
error
property
Expose the stored failure exception, or None for success.
Returns:
| Type | Description |
|---|---|
Exception | None
|
The exception stored by |
success
classmethod
Create an Ok containing value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
Successful value to store. |
required |
Returns:
| Type | Description |
|---|---|
Result[T]
|
A new successful result. |
failure
classmethod
Create an Err containing error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception
|
The exception stored in a failed result. |
required |
Returns:
| Type | Description |
|---|---|
Result[T]
|
A new failed result typed for the caller's expected success value. |
of
classmethod
Call a zero-argument function and capture ordinary exceptions as failure.
A normal return becomes Ok; an Exception becomes Err.
Exceptions outside the Exception hierarchy, such as KeyboardInterrupt, propagate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[], T]
|
Zero-argument computation to evaluate immediately. |
required |
Returns:
| Type | Description |
|---|---|
Result[T]
|
|
is_success
Report whether this instance is an Ok.
Returns:
| Type | Description |
|---|---|
bool
|
|
is_failure
Report whether this instance is an Err.
Returns:
| Type | Description |
|---|---|
bool
|
|
map
Map an Ok value while preserving an existing Err.
The mapper runs only for success. Its return value becomes a new Ok, and any ordinary
exception it raises becomes Err. A failed result returns itself unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
Callable[[T], R]
|
Callable applied to a successful value. |
required |
Returns:
| Type | Description |
|---|---|
Result[R]
|
The mapped success, captured mapper failure, or original failed result. |
and_then
Chain an Ok through a result-returning callable and bypass an Err.
Exceptions raised while mapping a success are captured as Err. The mapper's return
value is passed through without runtime type validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
Callable[[T], Result[R]]
|
Result-returning callable applied only to a successful value. |
required |
Returns:
| Type | Description |
|---|---|
Result[R]
|
The mapper's result, a captured mapper exception, or the original failure. |
flat_map
Alias and_then() for chaining result computations.
The callable runs only for success; existing failures pass through unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
Callable[[T], Result[R]]
|
Result-returning callable applied only to a successful value. |
required |
Returns:
| Type | Description |
|---|---|
Result[R]
|
Exactly |
map_err
Map an Err exception while preserving an existing Ok.
Mapper exceptions are not captured; they propagate to the caller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
Callable[[Exception], Exception]
|
Callable that converts the stored exception. |
required |
Returns:
| Type | Description |
|---|---|
Result[T]
|
A new |
map_error
on_success
Run a side effect for Ok and return this same result instance.
The action is skipped for Err; exceptions raised by the action propagate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Callable[[T], None]
|
Callable invoked with the successful value. |
required |
Returns:
| Type | Description |
|---|---|
Result[T]
|
|
on_failure
Run a side effect for Err and return this same result instance.
The action is skipped for Ok; exceptions raised by the action propagate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Callable[[Exception], None]
|
Callable invoked with the stored exception. |
required |
Returns:
| Type | Description |
|---|---|
Result[T]
|
|
unwrap
Extract an Ok value or raise the exception stored by Err.
Returns:
| Type | Description |
|---|---|
T
|
The successful value. |
Raises:
| Type | Description |
|---|---|
Exception
|
The stored failure exception when this result is unsuccessful. |
get_or_throw
Alias unwrap() for extracting success or raising failure.
Returns:
| Type | Description |
|---|---|
T
|
The successful value. |