Skip to content

Result

Result type implementation inspired by Rust's Result enum.

result

Result type implementation inspired by Rust's Result enum.

ResultAccessError

Bases: Error

Exception raised when trying to access value or err from an inappropriate Result variant.

Source code in src/forging_blocks/foundation/result.py
class ResultAccessError(Error):
    """Exception raised when trying to access value or err from an inappropriate Result variant."""

    def __init__(self, message: ErrorMessage | None = None) -> None:
        if message is None:
            message = ErrorMessage("Invalid access on Result type.")
        self._error_message = message
        super().__init__(message)

    def __str__(self) -> str:
        """Readable string representation."""
        return self._error_message.value

    @property
    def message(self) -> ErrorMessage:
        """Return the stored message as a string."""
        return self._error_message

    @classmethod
    def cannot_access_value(cls) -> ResultAccessError:
        """Create an error for accessing value from an Err Result."""
        return cls(ErrorMessage("Cannot access value from an Err Result."))

    @classmethod
    def cannot_access_error(cls) -> ResultAccessError:
        """Create an error for accessing error from an Ok Result."""
        return cls(ErrorMessage("Cannot access error from an Ok Result."))

message: ErrorMessage property

Return the stored message as a string.

__str__() -> str

Readable string representation.

Source code in src/forging_blocks/foundation/result.py
def __str__(self) -> str:
    """Readable string representation."""
    return self._error_message.value

cannot_access_value() -> ResultAccessError classmethod

Create an error for accessing value from an Err Result.

Source code in src/forging_blocks/foundation/result.py
@classmethod
def cannot_access_value(cls) -> ResultAccessError:
    """Create an error for accessing value from an Err Result."""
    return cls(ErrorMessage("Cannot access value from an Err Result."))

cannot_access_error() -> ResultAccessError classmethod

Create an error for accessing error from an Ok Result.

Source code in src/forging_blocks/foundation/result.py
@classmethod
def cannot_access_error(cls) -> ResultAccessError:
    """Create an error for accessing error from an Ok Result."""
    return cls(ErrorMessage("Cannot access error from an Ok Result."))

Result

Bases: Generic[ResultType, ErrorType], Protocol

A type that represents either a success (Ok) or an error (Err).

Source code in src/forging_blocks/foundation/result.py
class Result(Generic[ResultType, ErrorType], Protocol):
    """A type that represents either a success (Ok) or an error (Err)."""

    @property
    def is_ok(self) -> bool:
        """Guard method to check if that Result is an ok."""
        ...

    @property
    def is_err(self) -> bool:
        """Guard method to check if that Result is an err."""
        ...

    @property
    def value(self) -> ResultType | None:
        """Method to return the actual value. May raise an error if Result is an Err."""
        ...

    @property
    def error(self) -> ErrorType | None:
        """Method to return the actual error. May raise an error if Result is an Ok."""
        ...

is_ok: bool property

Guard method to check if that Result is an ok.

is_err: bool property

Guard method to check if that Result is an err.

value: ResultType | None property

Method to return the actual value. May raise an error if Result is an Err.

error: ErrorType | None property

Method to return the actual error. May raise an error if Result is an Ok.

Ok

Bases: Result[ResultType, ErrorType], Generic[ResultType, ErrorType]

Represents a successful result.

Source code in src/forging_blocks/foundation/result.py
class Ok(Result[ResultType, ErrorType], Generic[ResultType, ErrorType]):
    """Represents a successful result."""

    __match_args__ = ("_value",)

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

    def __str__(self) -> str:
        """Return a string representation of the Ok result."""
        return f"Ok({self._value})"

    def __repr__(self) -> str:
        """Return a string representation of the Ok result."""
        return f"Ok({self._value!r})"

    def __eq__(self, other: object) -> bool:
        """Check equality with another Ok result."""
        return isinstance(other, Ok) and self._value == other._value

    def __hash__(self) -> int:
        """Return the hash of the Ok result."""
        return hash(self._value)

    @property
    def is_err(self) -> bool:
        """Check if the result is an error."""
        return False

    @property
    def is_ok(self) -> bool:
        """Check if the result is ok."""
        return True

    @property
    def value(self) -> ResultType:
        """Get the successful value."""
        return self._value

    @property
    def error(self) -> None:
        """Attempting to get error from an Ok result raises an error."""
        raise ResultAccessError.cannot_access_error()

is_err: bool property

Check if the result is an error.

is_ok: bool property

Check if the result is ok.

value: ResultType property

Get the successful value.

error: None property

Attempting to get error from an Ok result raises an error.

__str__() -> str

Return a string representation of the Ok result.

Source code in src/forging_blocks/foundation/result.py
def __str__(self) -> str:
    """Return a string representation of the Ok result."""
    return f"Ok({self._value})"

__repr__() -> str

Return a string representation of the Ok result.

Source code in src/forging_blocks/foundation/result.py
def __repr__(self) -> str:
    """Return a string representation of the Ok result."""
    return f"Ok({self._value!r})"

__eq__(other: object) -> bool

Check equality with another Ok result.

Source code in src/forging_blocks/foundation/result.py
def __eq__(self, other: object) -> bool:
    """Check equality with another Ok result."""
    return isinstance(other, Ok) and self._value == other._value

__hash__() -> int

Return the hash of the Ok result.

Source code in src/forging_blocks/foundation/result.py
def __hash__(self) -> int:
    """Return the hash of the Ok result."""
    return hash(self._value)

Err

Bases: Result[ResultType, ErrorType], Generic[ResultType, ErrorType]

Represents an error result.

Source code in src/forging_blocks/foundation/result.py
class Err(Result[ResultType, ErrorType], Generic[ResultType, ErrorType]):
    """Represents an error result."""

    __match_args__ = ("_error",)

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

    def __repr__(self) -> str:
        """Return a string representation of the Err result."""
        return f"Err({self._error!r})"

    def __str__(self) -> str:
        """Return a string representation of the Err result."""
        return f"Err({self._error})"

    def __eq__(self, other: object) -> bool:
        """Check equality with another Err result."""
        return isinstance(other, Err) and self._error == other._error

    def __hash__(self) -> int:
        """Return the hash of the Err result."""
        return hash(self._error)

    @property
    def is_err(self) -> bool:
        """Check if the result is an error."""
        return True

    @property
    def is_ok(self) -> bool:
        """Check if the result is ok."""
        return False

    @property
    def value(self) -> None:
        """Attempting to get value from an Err result raises an error."""
        raise ResultAccessError.cannot_access_value()

    @property
    def error(self) -> ErrorType:
        """Get the error value."""
        return self._error

is_err: bool property

Check if the result is an error.

is_ok: bool property

Check if the result is ok.

value: None property

Attempting to get value from an Err result raises an error.

error: ErrorType property

Get the error value.

__repr__() -> str

Return a string representation of the Err result.

Source code in src/forging_blocks/foundation/result.py
def __repr__(self) -> str:
    """Return a string representation of the Err result."""
    return f"Err({self._error!r})"

__str__() -> str

Return a string representation of the Err result.

Source code in src/forging_blocks/foundation/result.py
def __str__(self) -> str:
    """Return a string representation of the Err result."""
    return f"Err({self._error})"

__eq__(other: object) -> bool

Check equality with another Err result.

Source code in src/forging_blocks/foundation/result.py
def __eq__(self, other: object) -> bool:
    """Check equality with another Err result."""
    return isinstance(other, Err) and self._error == other._error

__hash__() -> int

Return the hash of the Err result.

Source code in src/forging_blocks/foundation/result.py
def __hash__(self) -> int:
    """Return the hash of the Err result."""
    return hash(self._error)