Skip to content

result

Result

Bases: t.Generic[T, E], abc.ABC

Represents a potential Ok or Err result.

Note

This class can not be instantiated, only its variants can.

Source code in unkey/result.py
class Result(t.Generic[T, E], abc.ABC):
    """Represents a potential `Ok` or `Err` result.

    Note:
        This class can not be instantiated, only its variants can.
    """

    __slots__ = ("_error", "_value")

    def __repr__(self) -> str:
        inner = self._value if self.is_ok else self._error  # type: ignore [attr-defined]
        return f"{self.__class__.__name__}({inner})"

    @property
    @abc.abstractmethod
    def is_ok(self) -> bool:
        """`True` if this result is the `Ok` variant."""

    @property
    @abc.abstractmethod
    def is_err(self) -> bool:
        """`True` if this result is the `Err` variant."""

    @abc.abstractmethod
    def unwrap(self) -> T:
        """Unwraps the result to produce the value.

        Returns:
            The unwrapped value.

        Raises:
            UnwrapError: If the result was an `Err` and not `Ok`.
        """

    @abc.abstractmethod
    def unwrap_err(self) -> E:
        """Unwraps the result to produce the error.

        Returns:
            The unwrapped error.

        Raises:
            UnwrapError: If the result was `Ok` and not an `Err`.
        """

is_err abstractmethod property

is_err: bool

True if this result is the Err variant.

is_ok abstractmethod property

is_ok: bool

True if this result is the Ok variant.

unwrap abstractmethod

unwrap() -> T

Unwraps the result to produce the value.

Returns:

Type Description
T

The unwrapped value.

Raises:

Type Description
UnwrapError

If the result was an Err and not Ok.

Source code in unkey/result.py
@abc.abstractmethod
def unwrap(self) -> T:
    """Unwraps the result to produce the value.

    Returns:
        The unwrapped value.

    Raises:
        UnwrapError: If the result was an `Err` and not `Ok`.
    """

unwrap_err abstractmethod

unwrap_err() -> E

Unwraps the result to produce the error.

Returns:

Type Description
E

The unwrapped error.

Raises:

Type Description
UnwrapError

If the result was Ok and not an Err.

Source code in unkey/result.py
@abc.abstractmethod
def unwrap_err(self) -> E:
    """Unwraps the result to produce the error.

    Returns:
        The unwrapped error.

    Raises:
        UnwrapError: If the result was `Ok` and not an `Err`.
    """