Renders a result case by case: each on_* handles the cases it
matches unless an earlier one already did (Effect's builder). The
output type grows with each handler; Python cannot remove handled
cases from the type, so exhaustive is render (an unhandled
failure raises its squashed cause, as Effect's does).
Attributes#
| Attribute | Type | Description |
|---|---|---|
result | = result |
Functions#
__init__#
__init__(result) -> None| Parameter | Type | Description |
|---|---|---|
result | AsyncResult[A_co, E_co] |
def __init__(self, result: AsyncResult[A_co, E_co]) -> None:
self.result = result
self._output: Option[Any] = Noneon_waiting#
on_waiting[B](f) -> Builder[Out_co | B, A_co, E_co]| Parameter | Type | Description |
|---|---|---|
f | Callable[[AsyncResult[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_waiting[B](
self, f: Callable[[AsyncResult[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
return self._when(lambda r: r.waiting, lambda r: Some(f(r)))on_initial_or_waiting#
on_initial_or_waiting[B](f) -> Builder[Out_co | B, A_co, E_co]| Parameter | Type | Description |
|---|---|---|
f | Callable[[AsyncResult[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_initial_or_waiting[B](
self, f: Callable[[AsyncResult[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
return self._when(lambda r: isinstance(r, Initial) or r.waiting, lambda r: Some(f(r)))on_initial#
on_initial[B](f) -> Builder[Out_co | B, A_co, E_co]| Parameter | Type | Description |
|---|---|---|
f | Callable[[Initial[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_initial[B](
self, f: Callable[[Initial[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
return self._when(is_initial, lambda r: Some(f(r)))on_success#
on_success[B](f) -> Builder[Out_co | B, A_co, E_co]| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co, Success[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_success[B](
self, f: Callable[[A_co, Success[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
return self._when(is_success, lambda r: Some(f(r.value, r)))on_failure#
on_failure[B](f) -> Builder[Out_co | B, A_co, E_co]| Parameter | Type | Description |
|---|---|---|
f | Callable[[Cause[E_co], Failure[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_failure[B](
self, f: Callable[[Cause[E_co], Failure[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
return self._when(is_failure, lambda r: Some(f(r.cause, r)))on_error#
on_error[B](f) -> Builder[Out_co | B, A_co, E_co]| Parameter | Type | Description |
|---|---|---|
f | Callable[[E_co, Failure[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_error[B](
self, f: Callable[[E_co, Failure[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
return self.on_error_if(lambda _: True, f)on_error_if#
on_error_if[B](predicate, f) -> Builder[Out_co | B, A_co, E_co]| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[E_co], bool] | |
f | Callable[[E_co, Failure[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_error_if[B](
self, predicate: Callable[[E_co], bool], f: Callable[[E_co, Failure[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
def handle(result: Failure[A_co, E_co]) -> Option[object]:
found = error(result)
if found is None or not predicate(found.value):
return None
return Some(f(found.value, result))
return self._when(is_failure, handle)on_error_tag#
on_error_tag[T, B](tag, f) -> Builder[Out_co | B, A_co, E_co]Handle a typed error that is an instance of tag (pyfect's
tags are classes).
| Parameter | Type | Description |
|---|---|---|
tag | type[T] | tuple[type[T], ...] | |
f | Callable[[T, Failure[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_error_tag[T, B](
self, tag: type[T] | tuple[type[T], ...], f: Callable[[T, Failure[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
"""Handle a typed error that is an instance of ``tag`` (pyfect's
tags are classes)."""
def handle(result: Failure[A_co, E_co]) -> Option[object]:
found = error(result)
if found is None or not isinstance(found.value, tag):
return None
return Some(f(found.value, result))
return self._when(is_failure, handle)on_defect#
on_defect[B](f) -> Builder[Out_co | B, A_co, E_co]| Parameter | Type | Description |
|---|---|---|
f | Callable[[object, Failure[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_defect[B](
self, f: Callable[[object, Failure[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
def handle(result: Failure[A_co, E_co]) -> Option[object]:
defect = result.cause.find_defect()
return Some(f(defect.value, result)) if isinstance(defect, Ok) else None
return self._when(is_failure, handle)on_interrupt#
on_interrupt[B](f) -> Builder[Out_co | B, A_co, E_co]| Parameter | Type | Description |
|---|---|---|
f | Callable[[frozenset[int], Failure[A_co, E_co]], B] |
Returns Builder[Out_co | B, A_co, E_co]
def on_interrupt[B](
self, f: Callable[[frozenset[int], Failure[A_co, E_co]], B]
) -> Builder[Out_co | B, A_co, E_co]:
def handle(result: Failure[A_co, E_co]) -> Option[object]:
interruptors = result.cause.filter_interruptors()
return Some(f(interruptors.value, result)) if isinstance(interruptors, Ok) else None
return self._when(is_failure, handle)or_else#
or_else[B](or_else) -> Out_co | B| Parameter | Type | Description |
|---|---|---|
or_else | Callable[[], B] |
Returns Out_co | B
def or_else[B](self, or_else: Callable[[], B]) -> Out_co | B:
return or_else() if self._output is None else self._output.valueor_none#
or_none() -> Out_co | NoneReturns Out_co | None
def or_none(self) -> Out_co | None:
return None if self._output is None else self._output.valuerender#
render() -> Out_co | NoneReturns Out_co | None
def render(self) -> Out_co | None:
if self._output is not None:
return self._output.value
if isinstance(self.result, Failure):
raise self.result.cause.raisable()
return Noneexhaustive#
exhaustive() -> Out_corender, for a builder that handles every case.
Returns Out_co
def exhaustive(self) -> Out_co:
"""``render``, for a builder that handles every case."""
return self.render() # pyright: ignore[reportReturnType]