eotransform.result.Result

class Result(value: T | None = None, error: E | None = None, ignored: E | None = None)[source]

Bases: Generic[T, E]

Allows to hand exceptions across thread boundaries conveniently by wrapping them into a result object. Use the classmethod factories to produce expressive code i.e.:

>>> def everything_went_fine():
...     return Result.ok(42)
>>> def something_went_wrong():
...     return Result.error(RuntimeError("Something bad has happened"))
>>> everything_went_fine().unwrap()
42
>>> something_went_wrong().is_error()
True
>>> something_went_wrong().unwrap()
Traceback (most recent call last):
  ...
RuntimeError: Something bad has happened

@param value: value of a valid result @param error: error of an invalid result @param ignored: ignored error

Methods

error

Factory method creating an invalid Result object @param error: error which caused the result to be invalid @return: Result object containing an error

ignore

Ignore specified exception errors and return new Result object with ignored errors if they match.

ignored

Factory method creating an error Result object where the error has been ignored @param error: error which has been ignored @return: Result object containing an ignored error

is_error

Returns true if an error has been set, and the Result object is invalid @return: boolean

is_ignored

Returns true if an error has been ignored, and the Result object is invalid @return: boolean

ok

Factory method creating a valid Result object @param value: value of the valid result @return: Result object containing the value

report_error

Report the error's string representation.

unwrap

Extract the value of a valid Result object, and throws the set error if it is invalid i.e.: @return: value of valid object

classmethod error(error: E) Result[source]

Factory method creating an invalid Result object @param error: error which caused the result to be invalid @return: Result object containing an error

>>> Result.error(RuntimeError("Something bad has happened"))
Result(error=RuntimeError('Something bad has happened'))
ignore(exceptions: Set[Type[E]]) Result[source]

Ignore specified exception errors and return new Result object with ignored errors if they match. @param exceptions: set of exception types to be ignored @return: new result object with exceptions ignored if they match

>>> Result.error(RuntimeError("An error to be ignored")).ignored({RuntimeError}).unwrap()
classmethod ignored(error: E) Result[source]

Factory method creating an error Result object where the error has been ignored @param error: error which has been ignored @return: Result object containing an ignored error

>>> Result.ignored(RuntimeError("Something bad has happened"))
Result(ignored=RuntimeError('Something bad has happened'))
is_error() bool[source]

Returns true if an error has been set, and the Result object is invalid @return: boolean

is_ignored() bool[source]

Returns true if an error has been ignored, and the Result object is invalid @return: boolean

classmethod ok(value: T) Result[source]

Factory method creating a valid Result object @param value: value of the valid result @return: Result object containing the value

>>> Result.ok(42)
Result(value=42)
report_error() str[source]

Report the error’s string representation. @return: string representing the error

>>> Result.error(RuntimeError("Error report")).report_error()
'Error report'
unwrap() T[source]

Extract the value of a valid Result object, and throws the set error if it is invalid i.e.: @return: value of valid object

>>> Result.ok(42).unwrap()
42
>>> Result.error(RuntimeError("Something bad has happened")).unwrap()
Traceback (most recent call last):
...
RuntimeError: Something bad has happened