Skip to content

Ok

Ok variant of the Result type — represents a successful computation.

Wrap a value with Ok(value) and chain operations without fear of
:class:Err short-circuits leaking into your logic. This is the right
side of the Either monad (the Right in Haskell / Scala).

ok

Ok variant of the Result type — represents a successful computation.

Wrap a value with Ok(value) and chain operations without fear of
:class:Err short-circuits leaking into your logic. This is the right
side of the Either monad (the Right in Haskell / Scala).

Ok

Bases: Result[ValueType, ErrorType]

Represents a successful result, holding a value of type ValueType.

Source code in src/forging_blocks/foundation/result/ok.py
class Ok[ValueType, ErrorType](Result[ValueType, ErrorType]):
    """Represents a successful result, holding a value of type ``ValueType``."""

    __match_args__ = ("_value",)

    def __init__(self, value: ValueType) -> None:
        """Wrap ``value`` as a successful :class:`Result`."""
        self._value = value

    def __repr__(self) -> str:
        """Return a debug-friendly repr like ``Ok(value)``."""
        return f"Ok({self._value!r})"

    def __str__(self) -> str:
        """Return a user-friendly string like ``Ok(value)``."""
        return f"Ok({self._value})"

    def __eq__(self, other: object) -> bool:
        """Two Oks are equal when their wrapped values are equal."""
        if not isinstance(other, Ok):
            return False
        return self._value == cast(Ok[ValueType, ErrorType], other)._value

    def __hash__(self) -> int:
        """Hash based on the wrapped value."""
        return hash(self._value)

    @property
    def is_ok(self) -> bool:
        """Always ``True`` — this variant holds a success value."""
        return True

    @property
    def is_err(self) -> bool:
        """Always ``False`` — this variant does not hold an error."""
        return False

    @property
    def value(self) -> ValueType:
        """The wrapped success value."""
        return self._value

    @property
    def error(self) -> ErrorType:
        """Raises :class:`ResultAccessError` — there is no error to access."""
        raise ResultAccessError.cannot_access_error()

    def map[MappedValueType](
        self,
        fn: Callable[[ValueType], MappedValueType],
    ) -> Result[MappedValueType, ErrorType]:
        """Apply ``fn`` to the wrapped value and wrap the result in a new Ok.

        This is the Functor map — ``fmap`` in Haskell, ``.map`` on Scala's
        ``Option`` / ``Either``.
        """
        return Ok(fn(self._value))

    def map_error[MappedErrorType](
        self,
        fn: Callable[[ErrorType], MappedErrorType],
    ) -> Result[ValueType, MappedErrorType]:
        """Pass through unchanged — there is no error to transform."""
        return Ok(self._value)

    def flat_map[MappedValueType](
        self,
        fn: Callable[[ValueType], Result[MappedValueType, ErrorType]],
    ) -> Result[MappedValueType, ErrorType]:
        """Apply ``fn`` to the wrapped value and return its Result directly.

        Unlike :meth:`map`, ``fn`` itself returns a Result, so you can chain
        multiple fallible operations without nesting ``Ok(Ok(...))``.
        """
        return fn(self._value)

    def get_value_or(self, default: ValueType) -> ValueType:
        """Return the wrapped value, ignoring ``default``.

        Args:
            default: Ignored — this Result is a success.

        Returns:
            The unwrapped success value.
        """
        return self._value

    def get_value_or_else(
        self,
        fn: Callable[[ErrorType], ValueType],
    ) -> ValueType:
        """Return the wrapped value, ignoring ``fn``.

        Args:
            fn: Ignored — this Result is a success.

        Returns:
            The unwrapped success value.
        """
        return self._value

is_ok: bool property

Always True — this variant holds a success value.

is_err: bool property

Always False — this variant does not hold an error.

value: ValueType property

The wrapped success value.

error: ErrorType property

Raises :class:ResultAccessError — there is no error to access.

__init__(value: ValueType) -> None

Wrap value as a successful :class:Result.

Source code in src/forging_blocks/foundation/result/ok.py
def __init__(self, value: ValueType) -> None:
    """Wrap ``value`` as a successful :class:`Result`."""
    self._value = value

__repr__() -> str

Return a debug-friendly repr like Ok(value).

Source code in src/forging_blocks/foundation/result/ok.py
def __repr__(self) -> str:
    """Return a debug-friendly repr like ``Ok(value)``."""
    return f"Ok({self._value!r})"

__str__() -> str

Return a user-friendly string like Ok(value).

Source code in src/forging_blocks/foundation/result/ok.py
def __str__(self) -> str:
    """Return a user-friendly string like ``Ok(value)``."""
    return f"Ok({self._value})"

__eq__(other: object) -> bool

Two Oks are equal when their wrapped values are equal.

Source code in src/forging_blocks/foundation/result/ok.py
def __eq__(self, other: object) -> bool:
    """Two Oks are equal when their wrapped values are equal."""
    if not isinstance(other, Ok):
        return False
    return self._value == cast(Ok[ValueType, ErrorType], other)._value

__hash__() -> int

Hash based on the wrapped value.

Source code in src/forging_blocks/foundation/result/ok.py
def __hash__(self) -> int:
    """Hash based on the wrapped value."""
    return hash(self._value)

map(fn: Callable[[ValueType], MappedValueType]) -> Result[MappedValueType, ErrorType]

Apply fn to the wrapped value and wrap the result in a new Ok.

This is the Functor map — fmap in Haskell, .map on Scala's
Option / Either.

Source code in src/forging_blocks/foundation/result/ok.py
def map[MappedValueType](
    self,
    fn: Callable[[ValueType], MappedValueType],
) -> Result[MappedValueType, ErrorType]:
    """Apply ``fn`` to the wrapped value and wrap the result in a new Ok.

    This is the Functor map — ``fmap`` in Haskell, ``.map`` on Scala's
    ``Option`` / ``Either``.
    """
    return Ok(fn(self._value))

map_error(fn: Callable[[ErrorType], MappedErrorType]) -> Result[ValueType, MappedErrorType]

Pass through unchanged — there is no error to transform.

Source code in src/forging_blocks/foundation/result/ok.py
def map_error[MappedErrorType](
    self,
    fn: Callable[[ErrorType], MappedErrorType],
) -> Result[ValueType, MappedErrorType]:
    """Pass through unchanged — there is no error to transform."""
    return Ok(self._value)

flat_map(fn: Callable[[ValueType], Result[MappedValueType, ErrorType]]) -> Result[MappedValueType, ErrorType]

Apply fn to the wrapped value and return its Result directly.

Unlike :meth:map, fn itself returns a Result, so you can chain
multiple fallible operations without nesting Ok(Ok(...)).

Source code in src/forging_blocks/foundation/result/ok.py
def flat_map[MappedValueType](
    self,
    fn: Callable[[ValueType], Result[MappedValueType, ErrorType]],
) -> Result[MappedValueType, ErrorType]:
    """Apply ``fn`` to the wrapped value and return its Result directly.

    Unlike :meth:`map`, ``fn`` itself returns a Result, so you can chain
    multiple fallible operations without nesting ``Ok(Ok(...))``.
    """
    return fn(self._value)

get_value_or(default: ValueType) -> ValueType

Return the wrapped value, ignoring default.

Parameters:

Name Type Description Default
default ValueType

Ignored — this Result is a success.

required

Returns:

Type Description
ValueType

The unwrapped success value.

Source code in src/forging_blocks/foundation/result/ok.py
def get_value_or(self, default: ValueType) -> ValueType:
    """Return the wrapped value, ignoring ``default``.

    Args:
        default: Ignored — this Result is a success.

    Returns:
        The unwrapped success value.
    """
    return self._value

get_value_or_else(fn: Callable[[ErrorType], ValueType]) -> ValueType

Return the wrapped value, ignoring fn.

Parameters:

Name Type Description Default
fn Callable[[ErrorType], ValueType]

Ignored — this Result is a success.

required

Returns:

Type Description
ValueType

The unwrapped success value.

Source code in src/forging_blocks/foundation/result/ok.py
def get_value_or_else(
    self,
    fn: Callable[[ErrorType], ValueType],
) -> ValueType:
    """Return the wrapped value, ignoring ``fn``.

    Args:
        fn: Ignored — this Result is a success.

    Returns:
        The unwrapped success value.
    """
    return self._value