GitHub
All posts

Effect in TypeScript to Python: what carries over

Coming from Effect TS to Python? How Effect.gen, pipe, Layer, Context services, Schema and Stream map to pyfect, and where Python makes things different.

Tyler Gibbs
  • effect
  • effect-ts
  • typescript
  • python
  • pyright

If you have written Effect in TypeScript and now work in Python, most of what you know carries over. pyfect is a port of Effect v4 to Python (pip install pyfect-dev, import pyfect): the same data types, the same combinators under snake_case names, and the same runtime rules for errors, interruption and finalizers. This post maps Effect TS concepts to their Python names, then covers what Python changes and the few places where pyfect differs from Effect on purpose.

The introductory post explains what pyfect is for people who have never used Effect. This one assumes you have.

From Effect TS to Python, name by name

Here is a small Effect v4 program: a tagged error, a service, a generator function, a layer, and a handler.

greet.ts
import { Context, Data, Effect, Layer } from "effect"

class UserNotFound extends Data.TaggedError("UserNotFound")<{ readonly id: string }> {}

class Users extends Context.Service<Users, {
  readonly find: (id: string) => Effect.Effect<string, UserNotFound>
}>()("Users") {}

const greet = Effect.fn("greet")(function*(id: string) {
  const users = yield* Users
  const name = yield* users.find(id)
  return `Hello, ${name}`
})

const UsersLive = Layer.succeed(Users, {
  find: (id) => id === "1" ? Effect.succeed("Ada") : Effect.fail(new UserNotFound({ id }))
})

const program = (id: string) =>
  greet(id).pipe(
    Effect.catchTag("UserNotFound", (e) => Effect.succeed(`no user ${e.id}`)),
    Effect.provide(UsersLive)
  )

Effect.runPromise(program("2")).then(console.log)

And the same program in pyfect. pyright checks it in strict mode, and the build runs it:

greet.py
from dataclasses import dataclass
from typing import Protocol, assert_type

import pyfect as fx
from pyfect import layer


# Data.TaggedError("UserNotFound")<{ readonly id: string }>
@dataclass
class UserNotFound(fx.TaggedError):
    id: str


# The service's shape, then its key: Context.Service<Users, {...}>()("Users")
class UserStore(Protocol):
    def find(self, id: str) -> fx.Effect[str, UserNotFound]: ...


class Users(fx.Tag[UserStore]): ...


# Effect.fn("greet")(function* (id) { ... }), with yield from for yield*
@fx.fn("greet")
def greet(id: str):
    users = yield from Users()
    name = yield from users.find(id)
    return f"Hello, {name}"


assert_type(greet("1"), fx.Effect[str, UserNotFound, Users])


class InMemoryUsers:
    def find(self, id: str) -> fx.Effect[str, UserNotFound]:
        return fx.succeed("Ada") if id == "1" else fx.fail(UserNotFound(id))


# Layer.succeed(Users, {...})
UsersLive = layer.succeed(Users, InMemoryUsers())


# One function per .pipe call: catch_tag takes the class, not the "_tag" string.
def program(id: str) -> fx.Effect[str]:
    handled = greet(id).pipe(fx.catch_tag(UserNotFound, lambda e: fx.succeed(f"no user {e.id}")))
    return handled.pipe(fx.provide(UsersLive))


# Effect.runPromise(program) in TypeScript; no event loop needed here.
print(fx.run_blocking(program("1")))
print(fx.run_blocking(program("2")))

Line by line:

  • Effect.gen and Effect.fn become the decorators @fx.gen and @fx.fn, around a plain generator. yield* becomes yield from. pyright infers the type from the body, as TypeScript does: greet is Effect[str, UserNotFound, Users]. Write yield from, never a bare yield, or pyright loses the types.
  • Tagged errors are classes that subclass fx.TaggedError. A @dataclass gives them fields. You fail by yielding one, return (yield from UserNotFound(id)), much as yield* on an error fails in Effect. A raise inside a generator is a defect, not a typed failure.
  • Services. Effect 3's Context.Tag is Context.Service in v4. pyfect splits it in two: the shape is any class or Protocol, and the key is a subclass of fx.Tag[Shape]. You read it with yield from Users(). fx.Reference is the counterpart of Context.Reference, a key with a default value.
  • Layers are in pyfect.layer: layer.succeed, layer.sync, layer.effect, layer.merge, and fx.provide to provide one. They are memoized within one provide, as in Effect.
  • catchTag takes the error class instead of the "_tag" string, or a tuple of classes for several. Matching on classes is what lets pyright remove exactly that class from the type.
  • Running. There is no promise to unwrap. fx.run_blocking runs an effect on the calling thread with no event loop, await fx.run_async(eff) runs it inside asyncio, and fx.run_main is NodeRuntime.runMain: it handles SIGINT and SIGTERM and exits with a status.

Effect's Duration inputs carry over too: "5 seconds", a timedelta, or a bare number, which is milliseconds as in Effect.

Why pipe takes one function

The biggest change for a TypeScript user is pipe. In Effect you pass several functions to one .pipe(...). In pyfect each .pipe call takes exactly one, and you chain calls:

pipe_one.py
import pyfect as fx


def double(eff: fx.Effect[int]) -> fx.Effect[int]:
    return eff.map(lambda n: n * 2)


def describe(eff: fx.Effect[int]) -> fx.Effect[str]:
    return eff.map(lambda n: f"got {n}")


# Effect's eff.pipe(double, describe) is two calls here: one function per .pipe.
assert fx.run_blocking(fx.succeed(21).pipe(double).pipe(describe)) == "got 42"

# Two functions in one .pipe are a pyright error, and a TypeError if run anyway.
try:
    fx.succeed(21).pipe(double, describe)  # pyright: ignore[reportArgumentType]
except TypeError as error:
    print(error)

Without its ignore comment, the marked line is this pyright error, and the build fails if it ever stops being one:

Argument of type "(eff: Effect[int, Never, Never]) -> Effect[str, Never, Never]" cannot be
assigned to parameter "more" of type "Never" in function "pipe"

The reason is type inference. pyright narrows a generic call correctly for one step, but in a multi-step pipe it loses the narrowing after the first function. That matters most for the combinators that remove something from a type, catch_tag, provide and provide_service. They are also functions for .pipe, never methods: as methods, pyright would bind self first and could quietly remove the wrong thing. Everything else, map, flat_map, retry, timeout, fork, is a method, which reads more like Python.

Schema and Stream

pyfect.schema, imported as S, follows Effect's Schema: codecs with a decoded and an encoded side, checks, classes, tagged unions and JSON Schema output. pyfect.stream follows Stream: pull-based, with backpressure, and the same operators.

stream_schema.py
from typing import assert_type

import pyfect as fx
from pyfect import schema as S
from pyfect import stream


# Schema.Class<Reading>("Reading")({ sensor: Schema.String, celsius: Schema.Number })
class Reading(S.Class):
    sensor: str
    celsius: float


decode = S.decode_unknown_effect(S.from_json_string(Reading))

lines = ['{"sensor": "a", "celsius": 21.5}', '{"sensor": "b", "celsius": 19}']

# Stream.fromIterable(lines).pipe(Stream.mapEffect(decode, { concurrency: 2 }))
# becomes method calls: only combinators that remove a type are functions for .pipe.
readings = stream.from_iterable(lines).map_effect(decode, concurrency=2)
assert_type(readings, stream.Stream[Reading, S.SchemaError])

collected = fx.run_blocking_exit(readings.map(lambda r: f"{r.sensor}: {r.celsius}").run_collect())
print(collected)

Schema.Class<Reading>("Reading")({...}) becomes a subclass of S.Class whose annotations are its fields: str gives S.String, float gives S.Number, and S.field(schema) picks another schema. The decoders keep Effect's names in snake_case (decode_unknown_effect, decode_unknown_sync, encode_sync), and the effect form puts SchemaError in the error type, as the assert_type line shows. Stream operators are methods, with concurrency= as a keyword where Effect takes an options object. The schema guide and the streams guide list what is there.

What Python changes

Some things have a better Python spelling, and pyfect uses it.

python_features.py
from dataclasses import dataclass

import pyfect as fx
from pyfect.cause import Die, Fail
from pyfect.run import unwrap_group


@dataclass
class Overdrawn(fx.TaggedError):
    balance: int


class AuditLogDown(Exception):
    pass


def withdraw(amount: int) -> fx.Effect[int, Overdrawn]:
    return fx.succeed(100 - amount) if amount <= 100 else fx.fail(Overdrawn(100))


# Exit.match / Cause.match in TypeScript; Python's match takes them apart.
def describe(exit: fx.Exit[int, Overdrawn]) -> str:
    match exit:
        case fx.Success(left):
            return f"{left} left"
        case fx.Failure(fx.Cause([Fail(Overdrawn(balance))])):
            return f"only {balance} available"
        case fx.Failure(fx.Cause([Die(defect), *_])):
            return f"bug: {defect!r}"
        case fx.Failure(cause):
            return f"other: {cause!r}"


print(describe(fx.run_blocking_exit(withdraw(30))))
print(describe(fx.run_blocking_exit(withdraw(300))))

# A failure whose cleanup also crashed holds two errors. unwrap_group raises them
# as one ExceptionGroup, so except* handles each.
crashed = fx.run_blocking_exit(withdraw(300).ensuring(fx.die(AuditLogDown())))
try:
    unwrap_group(crashed)
except* Overdrawn as group:
    print("typed error:", group.exceptions)
except* AuditLogDown as group:
    print("defect:", group.exceptions)

# acquire_release as a context manager for code that isn't an effect.
session = fx.acquire_release(
    fx.sync(lambda: print("session opened") or "session-1"),
    lambda _s, exit: fx.sync(lambda: print(f"session closed after {type(exit).__name__}")),
)
with fx.managed(session) as s:
    print("using", s)

match instead of Exit.match and Cause.match. Exit, Cause, its reasons (Fail, Die, Interrupt), Option and Result all take part in Python's structural pattern matching, and pyright narrows each case. An Option is Some(value) or None, so it matches like any optional value.

Exception groups. A cause can hold several errors, such as a failure plus a crashed finalizer. run.unwrap_group(exit) raises them as one ExceptionGroup, so the traceback shows each one and except* picks them out. The ordinary run functions raise only the first error and attach the full cause as a note.

with blocks. fx.managed(eff) turns a scoped effect, such as an acquire_release, into a context manager. Code that isn't an effect gets the same release guarantee, and the release function still receives the Exit.

Threads and free-threading. Node gives Effect one thread, and pyfect keeps that model for fibers: all the fibers of one run take turns on that run's thread. Python has real threads, so pyfect adds fx.blocking for a blocking call on a thread pool and fx.on_thread for a whole effect on a new thread. On free-threaded Python 3.14, those can use several cores. The structured concurrency post goes into what that does and doesn't change.

contextvars. An effect reads its caller's contextvars, as Effect's runs follow Node's AsyncLocalStorage, so a request id set by a web framework is visible inside the effect and its forked fibers.

Where pyfect differs from Effect on purpose

The project's rule is to match Effect's behaviour wherever Python can express it. PARITY.md lists every difference with its reason. Besides the language mappings above, there are two kinds.

Security hardening. About forty places where pyfect keeps a protection that matching Effect would remove: request body and header limits, redirect and Origin checks, escaping in log lines, SSE events and SQL text, session and queue limits. The one you are most likely to meet is JSON depth. Every JSON parse in pyfect refuses text nested more than 512 arrays and objects deep, with the error that entry point gives malformed JSON. Effect's JSON.parse reads any depth, but Python code that walks a very deep value recursively, even ==, raises RecursionError. The schema guide lists each entry point and what it fails with.

Effect bugs not copied. A short list of Effect behaviours that look like bugs, checked under Node, which pyfect doesn't reproduce. For example, a MaxOpsBeforeYield of 2 or less never finishes in Effect; pyfect runs it as 3, the smallest budget that does.

Then there are the gaps. pyfect is alpha and covers about 95% of Effect v4. PARITY.md lists what isn't ported yet, row by row, so check it before you rely on a module you know from TypeScript.

Where to go next

Effects and generators covers running effects and the common mistakes, several of which are TypeScript habits that don't carry over. Services and layers covers tags and layers in depth, and typed errors covers causes, match and exception groups.