Skip to content

result

The Result type returned by every service method, along with its Ok and Err variants. See the result guide for how to branch on it.

This module contains the Result variants returned by all Client calls.

Correct usage

client = wom.Client()

await client.start()

result = await client.players.update_player("Jonxslays")

if result.is_ok:
    print(result.unwrap())
else:
    print(result.unwrap_err())

Incorrect usage

client = wom.Client()

await client.start()

result = await client.players.update_player("eeeeeeeeeeeee")

print(result.unwrap()) # <-- Exception raised
# Raises UnwrapError because the username was too long

Err

Bases: Result[T, E]

The Err variant of a Result.

Info

You will receive instances of this class as a result of calling Client methods, and should not have to instantiate it yourself.

Source code in wom/result.py
@t.final
class Err(Result[T, E]):
    """The [`Err`][wom.Err] variant of a [`Result`][wom.Result].

    !!! info

        You will receive instances of this class as a result of
        calling [`Client`][wom.Client] methods, and should not have to
        instantiate it yourself.
    """

    __slots__ = ()

    def __init__(self, error: E) -> None:
        self._error = error

    @property
    def is_ok(self) -> bool:
        """Always returns `False` for the [`Err`][wom.Err] variant."""
        return False

    @property
    def is_err(self) -> bool:
        """Always returns `True` for the [`Err`][wom.Err] variant."""
        return True

    def unwrap(self) -> T:
        """Always throws an exception for the [`Err`][wom.Err] variant.

        Raises
        ------
        UnwrapError
            Because the result was an [`Err`][wom.Err]
            variant.
        """
        raise errors.UnwrapError(f"Called unwrap on an error value - {self._error}")

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

        Returns
        -------
        E
            The unwrapped error.
        """
        return self._error

    def to_dict(self) -> t.Dict[str, t.Any]:
        """Converts the result into a dictionary.

        Returns
        -------
        dict[str, Any]
            The requested dictionary.
        """
        error = msgspec.to_builtins(self._error)
        return {"value": None, "error": error}

is_err property

is_err: bool

Always returns True for the Err variant.

is_ok property

is_ok: bool

Always returns False for the Err variant.

to_dict

to_dict() -> t.Dict[str, t.Any]

Converts the result into a dictionary.

Returns:

Type Description
dict[str, Any]

The requested dictionary.

Source code in wom/result.py
def to_dict(self) -> t.Dict[str, t.Any]:
    """Converts the result into a dictionary.

    Returns
    -------
    dict[str, Any]
        The requested dictionary.
    """
    error = msgspec.to_builtins(self._error)
    return {"value": None, "error": error}

unwrap

unwrap() -> T

Always throws an exception for the Err variant.

Raises:

Type Description
UnwrapError

Because the result was an Err variant.

Source code in wom/result.py
def unwrap(self) -> T:
    """Always throws an exception for the [`Err`][wom.Err] variant.

    Raises
    ------
    UnwrapError
        Because the result was an [`Err`][wom.Err]
        variant.
    """
    raise errors.UnwrapError(f"Called unwrap on an error value - {self._error}")

unwrap_err

unwrap_err() -> E

Unwraps the result to produce the error.

Returns:

Type Description
E

The unwrapped error.

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

    Returns
    -------
    E
        The unwrapped error.
    """
    return self._error

Ok

Bases: Result[T, E]

The Ok variant of a Result.

Info

You will receive instances of this class as a result of calling Client methods, and should not have to instantiate it yourself.

Source code in wom/result.py
@t.final
class Ok(Result[T, E]):
    """The [`Ok`][wom.Ok] variant of a [`Result`][wom.Result].

    !!! info

        You will receive instances of this class as a result of
        calling [`Client`][wom.Client] methods, and should not have to
        instantiate it yourself.
    """

    __slots__ = ()

    def __init__(self, value: T) -> None:
        self._value = value

    @property
    def is_ok(self) -> bool:
        """Always returns `True` for the [`Ok`][wom.Ok] variant."""
        return True

    @property
    def is_err(self) -> bool:
        """Always returns `False` for the [`Ok`][wom.Ok] variant."""
        return False

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

        Returns
        -------
        T
            The unwrapped value.
        """
        return self._value

    def unwrap_err(self) -> E:
        """Always throws an exception for the [`Ok`][wom.Ok] variant.

        Raises
        ------
        UnwrapError
            Because the result was an [`Ok`][wom.Ok]
            variant.
        """
        actual = self._value.__class__.__name__
        raise errors.UnwrapError(f"Called unwrap error on a non error value of type {actual!r}")

    def to_dict(self) -> t.Dict[str, t.Any]:
        """Converts the result into a dictionary.

        Returns
        -------
        dict[str, Any]
            The requested dictionary.
        """
        value = msgspec.to_builtins(self._value)
        return {"value": value, "error": None}

is_err property

is_err: bool

Always returns False for the Ok variant.

is_ok property

is_ok: bool

Always returns True for the Ok variant.

to_dict

to_dict() -> t.Dict[str, t.Any]

Converts the result into a dictionary.

Returns:

Type Description
dict[str, Any]

The requested dictionary.

Source code in wom/result.py
def to_dict(self) -> t.Dict[str, t.Any]:
    """Converts the result into a dictionary.

    Returns
    -------
    dict[str, Any]
        The requested dictionary.
    """
    value = msgspec.to_builtins(self._value)
    return {"value": value, "error": None}

unwrap

unwrap() -> T

Unwraps the result to produce the value.

Returns:

Type Description
T

The unwrapped value.

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

    Returns
    -------
    T
        The unwrapped value.
    """
    return self._value

unwrap_err

unwrap_err() -> E

Always throws an exception for the Ok variant.

Raises:

Type Description
UnwrapError

Because the result was an Ok variant.

Source code in wom/result.py
def unwrap_err(self) -> E:
    """Always throws an exception for the [`Ok`][wom.Ok] variant.

    Raises
    ------
    UnwrapError
        Because the result was an [`Ok`][wom.Ok]
        variant.
    """
    actual = self._value.__class__.__name__
    raise errors.UnwrapError(f"Called unwrap error on a non error value of type {actual!r}")

Result

Bases: Generic[T, E], ABC

Represents a potential Ok or Err result.

Note

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

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

    !!! note

        This class cannot be instantiated, only its variants can be.
    """

    __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`][wom.Ok] variant."""

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

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

        Returns
        -------
        T
            The unwrapped value.

        Raises
        ------
        UnwrapError
            If the result was an [`Err`][wom.Err] and not
            [`Ok`][wom.Ok].
        """

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

        Returns
        -------
        E
            The unwrapped error.

        Raises
        ------
        UnwrapError
            If the result was [`Ok`][wom.Ok] and not an
            [`Err`][wom.Err].
        """

    @abc.abstractmethod
    def to_dict(self) -> t.Dict[str, t.Any]:
        """Converts the result into a dictionary.

        If this result is [`Ok`][wom.Ok], the "value" property will be set.

        If this result is [`Err`][wom.Err], the "error" property will be set.

        Returns
        -------
        dict[str, Any]
            The requested dictionary.
        """

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.

to_dict abstractmethod

to_dict() -> t.Dict[str, t.Any]

Converts the result into a dictionary.

If this result is Ok, the "value" property will be set.

If this result is Err, the "error" property will be set.

Returns:

Type Description
dict[str, Any]

The requested dictionary.

Source code in wom/result.py
@abc.abstractmethod
def to_dict(self) -> t.Dict[str, t.Any]:
    """Converts the result into a dictionary.

    If this result is [`Ok`][wom.Ok], the "value" property will be set.

    If this result is [`Err`][wom.Err], the "error" property will be set.

    Returns
    -------
    dict[str, Any]
        The requested dictionary.
    """

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 wom/result.py
@abc.abstractmethod
def unwrap(self) -> T:
    """Unwraps the result to produce the value.

    Returns
    -------
    T
        The unwrapped value.

    Raises
    ------
    UnwrapError
        If the result was an [`Err`][wom.Err] and not
        [`Ok`][wom.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 wom/result.py
@abc.abstractmethod
def unwrap_err(self) -> E:
    """Unwraps the result to produce the error.

    Returns
    -------
    E
        The unwrapped error.

    Raises
    ------
    UnwrapError
        If the result was [`Ok`][wom.Ok] and not an
        [`Err`][wom.Err].
    """