GitHub
All posts

Typed errors in Python without exceptions

Typed errors in Python that pyright checks: pyfect puts expected failures in the type, so you handle or declare them. Compared with Result types and exceptions.

Tyler Gibbs
  • python
  • typed errors
  • pyright
  • error handling
  • result type

Python's type system tracks what a function returns, but not how it fails. A function typed -> str can raise anything, and pyright has no way to tell its callers. This post shows how pyfect gives Python typed errors: each expected failure is part of the function's type, and pyright makes every caller either handle it or pass it on. If you have searched for a Python Result type that works with pyright, this is a different answer to the same problem.

pyfect is a port of Effect to Python (pip install pyfect-dev, import pyfect). The error channel is the part most people notice first, so it is the part this post covers.

The problem with exceptions nothing tracks

Here is ordinary Python. load_user raises when a user is missing, and greeting calls it:

exceptions.py
class UserNotFound(Exception):
    pass


USERS = {"1": "Ada"}


def load_user(user_id: str) -> str:
    if user_id not in USERS:
        raise UserNotFound(user_id)
    return USERS[user_id]


def greeting(user_id: str) -> str:
    # pyright accepts this: nothing in load_user's type says it can raise.
    return f"Hello, {load_user(user_id)}"


print(greeting("1"))
try:
    greeting("2")
except UserNotFound as error:
    print(f"crashed: {error!r}")

pyright is happy with this file. greeting says it returns a str, and it does, until it doesn't. The only record that UserNotFound exists is the raise statement two calls down, plus maybe a docstring. Rename the exception, add a second one, or call greeting from a new place, and nothing points at the code that now needs to change. You find out from a traceback.

That is fine for bugs. A KeyError from a typo should crash. It is less fine for failures you expect: a missing user, an expired token, a payment the bank declined. Those are part of what the function does, and callers need to decide what happens next.

Errors in the type

In pyfect a function returns an Effect[A, E]: a description of work that succeeds with an A or fails with an E. Expected errors are classes that subclass fx.TaggedError. Inside a generator you fail by yielding the error, the same way you would yield from another effect:

tagged.py
from dataclasses import dataclass
from typing import assert_type

import pyfect as fx


@dataclass
class UserNotFound(fx.TaggedError):
    user_id: str


@dataclass
class Suspended(fx.TaggedError):
    user_id: str
    until: str


USERS = {"1": "Ada", "2": "Grace"}
SUSPENDED = {"2": "2026-12-01"}


@fx.fn
def load_user(user_id: str):
    if user_id not in USERS:
        return (yield from UserNotFound(user_id))  # yielding an error fails the effect
    if user_id in SUSPENDED:
        return (yield from Suspended(user_id, SUSPENDED[user_id]))
    return USERS[user_id]


@fx.fn
def greeting(user_id: str):
    name = yield from load_user(user_id)
    return f"Hello, {name}"


# Both errors flow up into greeting's type without a single annotation.
assert_type(greeting("1"), fx.Effect[str, UserNotFound | Suspended])

print(fx.run_blocking_exit(greeting("1")))
print(fx.run_blocking_exit(greeting("3")))

Nothing in greeting mentions an error, yet its type is Effect[str, UserNotFound | Suspended]. @fx.fn infers the union from every error the body yields, including the ones that come up through yield from load_user(...). The assert_type line is checked by pyright, so the type in that comment is what pyright infers, not what I hope it infers.

A failed run gives back a Failure holding a Cause, and the typed error is in it as a value. Nothing was raised.

One habit to learn: write return (yield from UserNotFound(user_id)), not raise UserNotFound(user_id). A raise inside the generator is treated as a defect (a bug): the error is not in the type, typed handlers skip it, and pyright can't warn you. The errors guide covers the difference.

Handling one error with catch_tag

fx.catch_tag(ErrorClass, handler) handles one error class and removes it from the type. Because it removes something, you use it through .pipe(...), not as a method. That isn't style: pyright can't infer the removal from a method call, and the curried form is what keeps the narrowing correct.

catch_tag.py
from dataclasses import dataclass
from typing import assert_type

import pyfect as fx


@dataclass
class UserNotFound(fx.TaggedError):
    user_id: str


@dataclass
class Suspended(fx.TaggedError):
    user_id: str
    until: str


USERS = {"1": "Ada", "2": "Grace"}
SUSPENDED = {"2": "2026-12-01"}


@fx.fn
def load_user(user_id: str):
    if user_id not in USERS:
        return (yield from UserNotFound(user_id))
    if user_id in SUSPENDED:
        return (yield from Suspended(user_id, SUSPENDED[user_id]))
    return USERS[user_id]


@fx.fn
def greeting(user_id: str):
    name = yield from load_user(user_id)
    return f"Hello, {name}"


def suspended(error: Suspended) -> fx.Effect[str]:
    return fx.succeed(f"Account {error.user_id} is suspended until {error.until}")


def page(user_id: str) -> fx.Effect[str]:
    # catch_tag handles one error class and removes it from the type.
    guest = greeting(user_id).pipe(fx.catch_tag(UserNotFound, lambda _: fx.succeed("Hello, guest")))
    assert_type(guest, fx.Effect[str, Suspended])
    return guest.pipe(fx.catch_tag(Suspended, suspended))


for user_id in ["1", "2", "3"]:
    print(fx.run_blocking(page(user_id)))

After the first catch_tag, the type is Effect[str, Suspended]. After the second, it is Effect[str], which is shorthand for no errors left. page is annotated to return exactly that, so if someone later adds a third error to load_user, page stops type-checking until they decide what it should do with it.

There are other handlers: catch_all for every typed error at once, map_error to turn one error into another, retry with a schedule, and .result() if you want an Ok or Err value to inspect yourself.

How pyright makes you handle or declare

Every typed error ends one of two ways: something handles it, or the function's type declares it. The checked file below shows both rules. Each marked line is a pyright error, and the project's build fails if one stops being an error, so these comments can't drift from what pyright really reports.

must_handle.py
from dataclasses import dataclass

import pyfect as fx


@dataclass
class UserNotFound(fx.TaggedError):
    user_id: str


USERS = {"1": "Ada"}


@fx.fn
def load_user(user_id: str):
    if user_id not in USERS:
        return (yield from UserNotFound(user_id))
    return USERS[user_id]


# Each line below is a pyright error; the build fails if one stops being one.


# A function that promises no errors can't return one that has them.
def render(user_id: str) -> fx.Effect[str]:
    return load_user(user_id)  # pyright: ignore[reportReturnType]


# run_blocking takes only effects whose errors are all handled.
name = fx.run_blocking(load_user("1"))  # pyright: ignore[reportArgumentType]

# run_blocking_exit takes any effect and hands back the failure as a value.
exit = fx.run_blocking_exit(load_user("2"))
assert isinstance(exit, fx.Failure)
assert exit.cause.find_error() == fx.Ok(UserNotFound("2"))

Without the ignore comments, pyright says:

must_handle.py:26:12 - error: Type "Effect[str, UserNotFound, Never]" is not assignable
  to return type "Effect[str, Never, Never]"
    Type "UserNotFound" is not assignable to type "Never" (reportReturnType)
must_handle.py:30:24 - error: Argument of type "Effect[str, UserNotFound, Never]" cannot
  be assigned to parameter "eff" of type "Effect[A@run_blocking, Never, Never]"
    Type "UserNotFound" is not assignable to type "Never" (reportArgumentType)

The first is the "declare" rule. A function that says it returns Effect[str] has promised callers there is nothing to handle, so returning something that can fail with UserNotFound is a type error. Widen the annotation to Effect[str, UserNotFound] or handle the error.

The second is the "handle" rule at the edge of your program. fx.run_blocking turns an effect into a plain value, and it only accepts effects with no errors left. If you want to run something that can still fail, fx.run_blocking_exit gives you the outcome as an Exit value to look at.

Wrapping code that raises

Most libraries you call will still raise. fx.try_ runs a function and turns what it raises into a typed error with catch=. If catch itself raises, that is a defect.

try_.py
import json
from dataclasses import dataclass
from typing import assert_type

import pyfect as fx


@dataclass
class BadConfig(fx.TaggedError):
    detail: str


def parse_config(text: str) -> fx.Effect[dict[str, object], BadConfig]:
    # try_ turns the exception you expect into a typed failure.
    return fx.try_(lambda: json.loads(text), catch=lambda e: BadConfig(str(e)))


port = parse_config('{"port": 8080}').map(lambda config: config["port"])
assert_type(port, fx.Effect[object, BadConfig])
print(fx.run_blocking(port.catch_all(lambda e: fx.succeed(f"bad config: {e.detail}"))))

broken = parse_config("{port: 8080}").map(lambda config: config["port"])
print(fx.run_blocking(broken.catch_all(lambda e: fx.succeed(f"bad config: {e.detail}"))))

Do this once, at the boundary where the library meets your code. From there inward, the failure is a BadConfig in the type, not a json.JSONDecodeError someone has to remember. For blocking calls there is fx.try_blocking, which runs on a thread so other work keeps going.

Python exceptions vs Result types vs an error channel

There are three ways to model expected failures in Python today. Each has a real cost.

Plain exceptions need no library and every Python developer already reads them. Tracebacks are good. The cost is the one this post started with: the type checker can't see them, so keeping callers in sync is up to discipline and tests. For scripts and for genuine bugs, exceptions are the right tool, and pyfect keeps them for bugs too.

Result types, such as Result from the returns library, make the error a value: a function returns Success(value) or Failure(error), and the type says which errors are possible. That gets you most of the type safety. Two things get in the way in practice. Chaining with bind keeps the error type the same across steps; the library's own docs point you to a separate unify function when steps fail differently, which is where unions of errors get awkward. And several of its typing features rely on a mypy plugin, so on pyright you get less of the checking. returns also has @safe to turn a raising function into one returning Result, which is close to fx.try_ above.

An error channel, which is what pyfect has, also makes errors values in the type. The differences from a Result type:

  • You write ordinary-looking code with yield from instead of chaining bind and map, and pyright unions the errors for you, as greeting showed.
  • Handlers remove exactly the error they handle (catch_tag), so the remaining type is always precise.
  • Expected failures and bugs are kept apart. A ZeroDivisionError inside an effect is a defect, which typed handlers don't see, so a bug never gets swallowed by a handler meant for NotFound.
  • The same Effect type carries the services a function needs and runs concurrently with timeouts and interruption. The errors are one part of a larger model.

The cost is that larger model. You are writing effects, not plain functions, and you need to learn yield from, @fx.fn, and when to use .pipe. pyfect needs Python 3.13 or newer, and its types are built around pyright's inference in strict mode. For a small script, that isn't worth it. For a service where the list of things that can fail keeps growing, it usually is.

Where to go next

The errors guide covers defects, exception groups, and matching on an Exit. If you have not installed pyfect yet, installation covers the pyright settings it expects. The same Effect type also tracks the services a function needs, which services and layers explains.