GitHub
All posts

Type-safe dependency injection in Python with services and layers

Type-safe dependency injection in Python: pyfect tracks the services each function needs, pyright flags a missing one, and tests swap in fake layers.

Tyler Gibbs
  • python
  • dependency injection
  • pyright
  • testing
  • services

Most Python dependency injection works at run time. You register providers in a container, ask it for a UserRepository, and find out whether you wired it correctly when that line runs. This post shows type-safe dependency injection in Python with pyfect's services and layers: each function's type lists the services it needs, pyright reports a missing one before you run anything, and tests swap in fake services without patching.

pyfect is a port of Effect to Python (pip install pyfect-dev, import pyfect). Its Effect[A, E, R] type has three parameters: the value, the errors, and the requirements. This post is about the third.

Defining a service

A service has two parts. Its shape is any class or Protocol. Its tag is the identity that code asks for, declared by subclassing fx.Tag. Keeping them apart means two tags can share one interface, a primary and a replica database for example.

Here is a sign-up flow that needs a user store and a mailer. The live layers use an in-memory SQLite database and print mail to the console, so the whole file runs as-is:

services.py
import sqlite3
from dataclasses import dataclass
from typing import Protocol, assert_type

import pyfect as fx
from pyfect import layer


@dataclass
class EmailTaken(fx.TaggedError):
    email: str


# A service is an interface plus a tag that effects ask for.
class UserStore(Protocol):
    def exists(self, email: str) -> fx.Effect[bool]: ...
    def insert(self, email: str) -> fx.Effect[int]: ...


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


class Mailer(Protocol):
    def send(self, to: str, subject: str) -> fx.Effect[None]: ...


class Mail(fx.Tag[Mailer]): ...


@fx.fn
def sign_up(email: str):
    users = yield from Users()
    if (yield from users.exists(email)):
        return (yield from EmailTaken(email))
    user_id = yield from users.insert(email)
    mailer = yield from Mail()
    yield from mailer.send(email, "Welcome!")
    return user_id


# The third type parameter lists what sign_up needs before it can run.
assert_type(sign_up("ada@example.com"), fx.Effect[int, EmailTaken, Users | Mail])


@dataclass
class SqliteUsers:
    db: sqlite3.Connection

    def exists(self, email: str) -> fx.Effect[bool]:
        query = "SELECT 1 FROM users WHERE email = ?"
        return fx.sync(lambda: self.db.execute(query, (email,)).fetchone() is not None)

    def insert(self, email: str) -> fx.Effect[int]:
        query = "INSERT INTO users (email) VALUES (?)"
        return fx.sync(lambda: self.db.execute(query, (email,)).lastrowid or 0)


def open_db() -> sqlite3.Connection:
    print("open database")
    db = sqlite3.connect(":memory:")
    db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT UNIQUE)")
    return db


def close_db(db: sqlite3.Connection) -> None:
    print("close database")
    db.close()


# The layer owns the connection: opened when the layer is built, closed when the program ends.
UsersLive = layer.effect(
    Users,
    fx.acquire_release(fx.sync(open_db), lambda db, _exit: fx.sync(lambda: close_db(db))).map(
        SqliteUsers
    ),
)


class ConsoleMailer:
    def send(self, to: str, subject: str) -> fx.Effect[None]:
        return fx.sync(lambda: print(f"mail to {to}: {subject}"))


MailLive = layer.succeed(Mail, ConsoleMailer())
AppLive = layer.merge(UsersLive, MailLive)


@fx.gen
def program():
    print((yield from sign_up("ada@example.com").exit()))
    print((yield from sign_up("ada@example.com").exit()))


fx.run_blocking(program.pipe(fx.provide(AppLive)))

A few things to notice.

sign_up asks for its dependencies with yield from Users() and yield from Mail(), at the point it needs them. There is no constructor taking a repository and a mailer, and no decorator. pyright infers Users | Mail as the requirements from those two lines, and the assert_type check pins it.

The requirements spread to callers the same way errors do. Any function that calls sign_up also needs Users | Mail until something provides them. So adding a new dependency deep in the call graph changes the types all the way up, and pyright shows you every place that now needs it.

Providing it with a layer

A layer builds services. layer.succeed(Mail, ConsoleMailer()) provides a value you already have. layer.effect(Users, ...) runs an effect to build one, and that effect can open resources. Here fx.acquire_release opens the SQLite connection and registers its close, so the connection lives exactly as long as the program using the layer. That is why the output starts with open database and ends with close database, after the second sign-up failed.

layer.merge(UsersLive, MailLive) combines the two, and fx.provide(AppLive) hands them to the program. Each layer is built once per fx.provide, however many services inside it ask for the same dependency, so keep layers in module-level constants and provide them once around the whole program.

Layers can depend on other layers, too. A UsersLive that needed a Db service to build would have the type Layer[Users, Never, Db], and UsersLive.pipe(layer.provide(DbLive)) would wire them together. The services guide walks through that graph.

pyright catches a missing dependency

The point of putting requirements in the type is what happens when you forget one. Here only the mailer is provided:

missing.py
from typing import Never, Protocol, assert_type

import pyfect as fx
from pyfect import layer


class Mailer(Protocol):
    def send(self, to: str, subject: str) -> fx.Effect[None]: ...


class Mail(fx.Tag[Mailer]): ...


class UserStore(Protocol):
    def insert(self, email: str) -> fx.Effect[int]: ...


class Users(fx.Tag[UserStore], key="Users"): ...


@fx.fn
def sign_up(email: str):
    users = yield from Users()
    user_id = yield from users.insert(email)
    mailer = yield from Mail()
    yield from mailer.send(email, "Welcome!")
    return user_id


class ConsoleMailer:
    def send(self, to: str, subject: str) -> fx.Effect[None]:
        return fx.sync(lambda: print(f"mail to {to}: {subject}"))


# provide removes exactly what the layer builds; Users is still needed.
half = sign_up("ada@example.com").pipe(fx.provide(layer.succeed(Mail, ConsoleMailer())))
assert_type(half, fx.Effect[int, Never, Users])

# pyright rejects the run. Ignore it and the program dies at the first `yield from Users()`.
exit = fx.run_blocking_exit(half)  # pyright: ignore[reportArgumentType]
assert isinstance(exit, fx.Failure)
print(exit.cause.defects[0])

fx.provide removes exactly what its layer builds, so half still needs Users. Running it is the error. Without the ignore comment, pyright reports:

missing.py:40:29 - error: Argument of type "Effect[int, Never, Users]" cannot be assigned
  to parameter "eff" of type "Effect[A@run_blocking_exit, E@run_blocking_exit, Never]"
    Type "Users" is not assignable to type "Never" (reportArgumentType)

The run functions only accept effects whose requirements are Never, meaning nothing left to provide. The snippet keeps the ignore comment so you can see what happens if you push past the error: the program starts, and the first yield from Users() dies with Service not found: Users. The project's build checks that the ignored line really is a pyright error, so this example can't quietly go stale.

The key="Users" on the tag is its identity, as in Effect: two tags with one key are one service. It is also the name shown in messages like that one. Without it, the key is the class's module and name, so tags declared in different places stay apart.

Testing with fake services

Since a service is a protocol, a fake is any class with the same methods. A test provides fake layers in place of the live ones, and nothing needs patching:

test_sign_up.py
from dataclasses import dataclass, field
from typing import Protocol

import pyfect as fx
from pyfect import layer, testing


@dataclass
class EmailTaken(fx.TaggedError):
    email: str


class UserStore(Protocol):
    def exists(self, email: str) -> fx.Effect[bool]: ...
    def insert(self, email: str) -> fx.Effect[int]: ...


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


class Mailer(Protocol):
    def send(self, to: str, subject: str) -> fx.Effect[None]: ...


class Mail(fx.Tag[Mailer]): ...


@fx.fn
def sign_up(email: str):
    users = yield from Users()
    if (yield from users.exists(email)):
        return (yield from EmailTaken(email))
    user_id = yield from users.insert(email)
    mailer = yield from Mail()
    yield from mailer.send(email, "Welcome!")
    return user_id


# Fakes: plain classes with the same shape as the real services.
@dataclass
class InMemoryUsers:
    emails: list[str] = field(default_factory=list[str])

    def exists(self, email: str) -> fx.Effect[bool]:
        return fx.sync(lambda: email in self.emails)

    def insert(self, email: str) -> fx.Effect[int]:
        return fx.sync(lambda: self.emails.append(email) or len(self.emails))


@dataclass
class RecordingMailer:
    sent: list[tuple[str, str]] = field(default_factory=list[tuple[str, str]])

    def send(self, to: str, subject: str) -> fx.Effect[None]:
        return fx.sync(lambda: self.sent.append((to, subject)))


@testing.effect
def test_a_new_user_gets_a_welcome_mail():
    mailer = RecordingMailer()
    fakes = layer.merge(layer.succeed(Users, InMemoryUsers()), layer.succeed(Mail, mailer))
    user_id = yield from sign_up("ada@example.com").pipe(fx.provide(fakes))
    assert user_id == 1
    assert mailer.sent == [("ada@example.com", "Welcome!")]


def already_there(email: str) -> fx.Effect[bool]:
    return fx.succeed(True)


@testing.effect
def test_a_taken_email_fails_and_sends_nothing():
    # layer.mock stubs only the members a test uses. Mail has none here,
    # so any send would die with UnimplementedError and fail the test.
    mocks = layer.merge(layer.mock(Users, exists=already_there), layer.mock(Mail))
    exit = yield from sign_up("ada@example.com").exit().pipe(fx.provide(mocks))
    testing.assert_exit_fail(exit, EmailTaken("ada@example.com"))

The first test builds two fakes by hand. InMemoryUsers and RecordingMailer are ordinary dataclasses that pyright checks against the protocols when they go into layer.succeed, so a fake whose send takes the wrong arguments is a type error, not a confusing test failure.

The second test uses layer.mock, which provides a service made of just the members you give it. Users gets an exists that always says yes. Mail gets nothing, so if sign_up tried to send a welcome mail to a taken address, send would die with UnimplementedError and the test would fail. That turns "sends nothing" into something the test checks, without writing a fake that counts calls. One caveat from the services guide: pyright doesn't check the names you pass to layer.mock, so a misspelt member leaves the real one unmocked. The defect it causes names the likely typo.

@testing.effect comes from pyfect's pytest plugin. It runs a generator test with a virtual clock, so a test of retries or timeouts takes no real time. For a layer that is expensive to build, such as a database in a container, testing.layer_fixture builds it once per pytest scope and lets tests declare which services they use; see testing.

Compared with other Python DI

Constructor injection by hand is the simplest thing that works, and pyright checks it fine: SignUp(users, mailer). It gets heavy as the graph grows, because every layer of the call stack has to accept and pass along dependencies it doesn't use itself. Services in pyfect skip the passing: a function deep in the stack asks for what it needs, and the type tells the top of the program what to provide.

Container libraries resolve providers by key or type when you ask for them. That is flexible, but a missing or mismatched binding shows up when it is resolved, at run time, often in a code path your tests didn't reach.

FastAPI's Depends is well liked and works well inside FastAPI. For tests it offers app.dependency_overrides, a dict from the original dependency function to its replacement. It works, but it's global state on the app, and nothing checks that the override returns the same type as the function it replaces.

What pyfect's approach costs: your code is written as effects, with yield from and @fx.fn, and you need pyright in strict mode to get the checking. It also helps to think in layers, which takes a little getting used to. In return, the type of every function says what it needs, a missing service is a type error, and swapping implementations for tests is a matter of providing a different layer.

Where to go next

The services and layers guide covers layer constructors, layer.provide between layers, fx.Reference for services with a default, and how layers are shared. The testing guide covers the pytest plugin and its virtual clock. Services pair with pyfect's typed errors, which use the same type to track failures.