Module pyfplib.result

Classes

class Err (error: ~E)
Expand source code
class Err(Result[T, E]):
    """Result constructor."""

    def __init__(self, error: E):
        super().__init__(error=error)

Result constructor.

Ancestors

Inherited members

class Ok (value: ~T)
Expand source code
class Ok(Result[T, E]):
    """Result constructor."""

    def __init__(self, value: T):
        super().__init__(value=value)

Result constructor.

Ancestors

Inherited members

class Result (*, value: ~T | None = None, error: ~E | None = None)
Expand source code
class Result(Generic[T, E]):
    __match_args__ = ("value",)

    def __init__(self, *, value: Optional[T] = None, error: Optional[E] = None):
        if error is None:
            self.__value = value
            self.__is_ok = True
        else:
            self.__value = error
            self.__is_ok = False

    @property
    def value(self) -> Union[T, E]:
        """Returns a raw value."""
        return self.__value

    def is_ok(self) -> bool:
        """Returns True if the option is Ok[T]."""
        return self.__is_ok

    def is_err(self) -> bool:
        """Returns True if the option is Err[T]."""
        return not self.__is_ok

    def if_ok(self, fn: Callable[[T], None]) -> "Result[T, E]":
        """\
        Calls fn function and returns itself.
        The fn function will be called if this option is Ok[T].
        """
        if self.is_ok():
            fn(self.__value)
        return self

    def if_err(self, fn: Callable[[E], None]) -> "Result[T, E]":
        """\
        Calls fn function and returns itself.
        The fn function will be called if this option is Err[E].
        """
        if self.is_err():
            fn(self.__value)
        return self

    def ok(self) -> Option[T]:
        """\
        Returns Some[T] if this option is Ok[T],
        otherwise it returns Nothing[T].
        """
        return Some(self.__value) if self.is_ok() else Nothing()

    def err(self) -> Option[E]:
        """\
        Returns Some[Exception] if this option is Err[E],
        otherwise it returns Nothing[Exception].
        """
        option: Option[Exception]
        if self.is_err():
            option = Some[Exception](cast(Exception, self.__value))
        else:
            option = Nothing[Exception]()
        return option

    def unwrap(self) -> T:
        """
        Extracts the contained value, raising UnwrapError if Err.

        Raises:
            UnwrapError: When attempting to unwrap Err
        """
        if self.is_err():
            msg = "called `Result.unwrap()` on a `Err` value"
            raise UnwrapError(msg)
        return self.__value

    def unwrap_err(self) -> E:
        """
        Extracts the contained error, raising UnwrapError if Ok.

        Raises:
            UnwrapError: When attempting to unwrap Ok
        """
        if self.is_ok():
            msg = "called `Result.unwrap_err()` on a `Ok` value"
            raise UnwrapError(msg)
        return self.__value

    def expect(self, message: str) -> T:
        """Returns contained value or raises ExpectedError"""
        if self.is_err():
            raise ExpectedError(message)
        return self.__value

    def expect_err(self, message: str) -> E:
        """Returns contained value or raises ExpectedError"""
        if self.is_ok():
            raise ExpectedError(message)
        return self.__value

    def unwrap_or(self, value: T) -> T:
        """Returns contained value or provided default if Err."""
        return self.__value if self.is_ok() is None else value

    def unwrap_err_or(self, value: E) -> E:
        """Returns contained value or provided default if Ok."""
        return self.__value if self.is_err() is None else value

    def map(self, fn: Callable[[T], U]) -> "Result[U, E]":
        """Maps an Result[T, E] to Result[U, E]"""
        result: Result[U, E]
        if self.is_ok():
            result = Ok[U, E](fn(cast(T, self.__value)))
        else:
            result = Err[U, E](cast(E, self.__value))
        return result

    def map_or(self, default: T, fn: Callable[[T], U]) -> "Result[U]":
        """Maps an Result[T] to Result[U]"""
        result: Result[U]
        if self.is_ok():
            result = fn(self.__value)
        else:
            result = fn(default)
        return result

    @staticmethod
    def try_call(fn: Callable[..., R], *args, **kwargs) -> "Result[R, E]":
        """Tries to call fn function and returns a result of the execution."""
        try:
            ret: R = fn(*args, **kwargs)
            return Ok[R, E](ret)
        except Exception as err:
            return Err[R, E](err)

Abstract base class for generic types.

On Python 3.12 and newer, generic classes implicitly inherit from Generic when they declare a parameter list after the class's name::

class Mapping[KT, VT]:
    def __getitem__(self, key: KT) -> VT:
        ...
    # Etc.

On older versions of Python, however, generic classes have to explicitly inherit from Generic.

After a class has been declared to be generic, it can then be used as follows::

def lookup_name[KT, VT](mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
    try:
        return mapping[key]
    except KeyError:
        return default

Ancestors

  • typing.Generic

Subclasses

Static methods

def try_call(fn: Callable[..., ~R], *args, **kwargs) ‑> Result[~R, ~E]
Expand source code
@staticmethod
def try_call(fn: Callable[..., R], *args, **kwargs) -> "Result[R, E]":
    """Tries to call fn function and returns a result of the execution."""
    try:
        ret: R = fn(*args, **kwargs)
        return Ok[R, E](ret)
    except Exception as err:
        return Err[R, E](err)

Tries to call fn function and returns a result of the execution.

Instance variables

prop value : ~T | ~E
Expand source code
@property
def value(self) -> Union[T, E]:
    """Returns a raw value."""
    return self.__value

Returns a raw value.

Methods

def err(self) ‑> Option[~E]
Expand source code
def err(self) -> Option[E]:
    """\
    Returns Some[Exception] if this option is Err[E],
    otherwise it returns Nothing[Exception].
    """
    option: Option[Exception]
    if self.is_err():
        option = Some[Exception](cast(Exception, self.__value))
    else:
        option = Nothing[Exception]()
    return option

Returns Some[Exception] if this option is Err[E], otherwise it returns Nothing[Exception].

def expect(self, message: str) ‑> ~T
Expand source code
def expect(self, message: str) -> T:
    """Returns contained value or raises ExpectedError"""
    if self.is_err():
        raise ExpectedError(message)
    return self.__value

Returns contained value or raises ExpectedError

def expect_err(self, message: str) ‑> ~E
Expand source code
def expect_err(self, message: str) -> E:
    """Returns contained value or raises ExpectedError"""
    if self.is_ok():
        raise ExpectedError(message)
    return self.__value

Returns contained value or raises ExpectedError

def if_err(self, fn: Callable[[~E], None]) ‑> Result[~T, ~E]
Expand source code
def if_err(self, fn: Callable[[E], None]) -> "Result[T, E]":
    """\
    Calls fn function and returns itself.
    The fn function will be called if this option is Err[E].
    """
    if self.is_err():
        fn(self.__value)
    return self

Calls fn function and returns itself. The fn function will be called if this option is Err[E].

def if_ok(self, fn: Callable[[~T], None]) ‑> Result[~T, ~E]
Expand source code
def if_ok(self, fn: Callable[[T], None]) -> "Result[T, E]":
    """\
    Calls fn function and returns itself.
    The fn function will be called if this option is Ok[T].
    """
    if self.is_ok():
        fn(self.__value)
    return self

Calls fn function and returns itself. The fn function will be called if this option is Ok[T].

def is_err(self) ‑> bool
Expand source code
def is_err(self) -> bool:
    """Returns True if the option is Err[T]."""
    return not self.__is_ok

Returns True if the option is Err[T].

def is_ok(self) ‑> bool
Expand source code
def is_ok(self) -> bool:
    """Returns True if the option is Ok[T]."""
    return self.__is_ok

Returns True if the option is Ok[T].

def map(self, fn: Callable[[~T], ~U]) ‑> Result[~U, ~E]
Expand source code
def map(self, fn: Callable[[T], U]) -> "Result[U, E]":
    """Maps an Result[T, E] to Result[U, E]"""
    result: Result[U, E]
    if self.is_ok():
        result = Ok[U, E](fn(cast(T, self.__value)))
    else:
        result = Err[U, E](cast(E, self.__value))
    return result

Maps an Result[T, E] to Result[U, E]

def map_or(self, default: ~T, fn: Callable[[~T], ~U]) ‑> Result[U]
Expand source code
def map_or(self, default: T, fn: Callable[[T], U]) -> "Result[U]":
    """Maps an Result[T] to Result[U]"""
    result: Result[U]
    if self.is_ok():
        result = fn(self.__value)
    else:
        result = fn(default)
    return result

Maps an Result[T] to Result[U]

def ok(self) ‑> Option[~T]
Expand source code
def ok(self) -> Option[T]:
    """\
    Returns Some[T] if this option is Ok[T],
    otherwise it returns Nothing[T].
    """
    return Some(self.__value) if self.is_ok() else Nothing()

Returns Some[T] if this option is Ok[T], otherwise it returns Nothing[T].

def unwrap(self) ‑> ~T
Expand source code
def unwrap(self) -> T:
    """
    Extracts the contained value, raising UnwrapError if Err.

    Raises:
        UnwrapError: When attempting to unwrap Err
    """
    if self.is_err():
        msg = "called `Result.unwrap()` on a `Err` value"
        raise UnwrapError(msg)
    return self.__value

Extracts the contained value, raising UnwrapError if Err.

Raises

UnwrapError
When attempting to unwrap Err
def unwrap_err(self) ‑> ~E
Expand source code
def unwrap_err(self) -> E:
    """
    Extracts the contained error, raising UnwrapError if Ok.

    Raises:
        UnwrapError: When attempting to unwrap Ok
    """
    if self.is_ok():
        msg = "called `Result.unwrap_err()` on a `Ok` value"
        raise UnwrapError(msg)
    return self.__value

Extracts the contained error, raising UnwrapError if Ok.

Raises

UnwrapError
When attempting to unwrap Ok
def unwrap_err_or(self, value: ~E) ‑> ~E
Expand source code
def unwrap_err_or(self, value: E) -> E:
    """Returns contained value or provided default if Ok."""
    return self.__value if self.is_err() is None else value

Returns contained value or provided default if Ok.

def unwrap_or(self, value: ~T) ‑> ~T
Expand source code
def unwrap_or(self, value: T) -> T:
    """Returns contained value or provided default if Err."""
    return self.__value if self.is_ok() is None else value

Returns contained value or provided default if Err.