GitHub

API reference for errors.

Typed errors.

Subclass TaggedError (usually as a @dataclass(eq=False); plain @dataclass works too, but pyright then thinks it unhashable) for domain errors. An instance is yieldable: yield from NotFound(key) fails the surrounding generator and adds NotFound to its error channel. Write return (yield from NotFound(key)) so the type checker knows the code after it is unreachable.

The class is the tag: catch_tag(NotFound, ...) matches by isinstance, so catching a base class catches its subclasses, and the types agree.

Like Effect's Data.TaggedError, errors compare and hash structurally (same class, equal fields or args), a message field is the string form, a cause field holding an exception becomes __cause__, and exit_code is the process status run_main exits with when the error ends the program. error_reported and the report_* attributes tune how run_main and the error reporters (pyfect.error_reporter) treat it.

Classes

Functions

filter_done

filter_done(cause) -> Result[Done[Any], Cause[E]]

Ok(done) if cause holds a Done failure and nothing else but interruptions; otherwise Err of the cause without its Done failures (Effect's Pull.filterDone).

ParameterTypeDescription
causeCause[E]

Returns Result[Done[Any], Cause[E]]

def filter_done[E](cause: Cause[E]) -> Result[Done[Any], Cause[E]]:
    """``Ok(done)`` if ``cause`` holds a ``Done`` failure and nothing else but
    interruptions; otherwise ``Err`` of the cause without its ``Done``
    failures (Effect's ``Pull.filterDone``)."""
    dones = [r.error for r in cause.reasons if _is_done(r)]
    if not dones:
        return Err(cause)
    if all(_is_done(r) or isinstance(r, Interrupt) for r in cause.reasons):
        return Ok(dones[0])
    return Err(from_reasons(r for r in cause.reasons if not _is_done(r)))