Skip to main content

pi-pilgrim

Dependency injection for Python. A port of Pilgrim, the successor to Typhoon.

  • Plain Python. An assembly is a class, and each component is a method that says how to make it. No decorators on your classes, no container configuration, no string lookups unless you choose a key, nothing generated.
  • A composition root. The key actors in your application, and how they're wired together, are written down in one place, as code your IDE can follow and refactor.
  • Lifecycles: shared, weak_shared, object_graph (Typhoon's idea: one instance for each graph that's built) and unshared.
  • Circular dependencies that are resolved after the graph is built, with configure= or PilgrimConfigurable.
  • Closing. An assembly closes the components it holds when it stops, newest first, as Spring closes its singletons: with, async with, close() or await aclose(). One whose teardown is async, an AsyncPilgrimAssembly, closes with await close() too.
  • Runtime arguments. An assembly method can take arguments, so it doubles as a factory that mixes the caller's values with the components it wires in.
  • Injection into top-level classes with Assembled, by type or by key.
  • Fast and thread-safe. A shared component resolves in about a third of a microsecond, and is made once however many threads ask for it.
  • No dependencies. Python 3.11 or newer.
pip install pi-pilgrim

Quick start

An assembly says what your components are and how they depend on each other. Each method returns one component, made with a lifecycle, and calls the assembly's other methods for what it needs:

from pipilgrim import PilgrimAssembly


class QuestAssembly(PilgrimAssembly):
    def knight(self) -> Knight:
        return self.unshared(lambda: Knight(quest=self.quest(), horse=self.horse()))

    def quest(self) -> Quest:
        return self.shared(lambda: HolyGrailQuest(map=self.map()))

    def horse(self) -> Horse:
        return self.object_graph(lambda: Horse(name="Bucephalus"))

    def map(self) -> Map:
        return self.shared(lambda: Map(region="Britain"))

Ask it for a component and you get the whole graph:

assembly = QuestAssembly()
knight = assembly.knight()  # a Knight, its shared Quest and Map, and a Horse

Your classes stay plain: Knight takes its quest and horse in its initializer and knows nothing about Pilgrim. To test it, pass it a fake.

Lifecycles

lifecycle each request for the component gets
shared the same instance, for the life of the assembly
weak_shared the same instance while something else holds it. Once nothing does, a new one
object_graph the same instance throughout one request, and a new one for the next
unshared a new instance

object_graph is for components that should be shared inside one graph but not between graphs. If a Knight and its Squire both ask for the horse while one knight is being built, they get the same horse. The next knight gets another.

weak_shared keeps a weak reference, so the instance must support one. Most classes do. int, str, list and dict don't, and Pilgrim says so when you try.

Runtime arguments

An assembly method can take arguments. They're part of the component's name, so a shared component made with different arguments is a different instance:

class QuestAssembly(PilgrimAssembly):
    def knight_named(self, name: str) -> Knight:
        return self.unshared(lambda: Knight(name=name, quest=self.quest()))

    def kingdom(self, region: str) -> Kingdom:
        return self.shared(lambda: Kingdom(region=region))


assembly.knight_named("Lancelot")     # a new Knight, with the shared quest
assembly.kingdom("Wessex")            # the same Kingdom on every call with "Wessex"

A shared, weak_shared or object_graph component's arguments must be hashable. An unshared one's needn't be. Arguments that are equal name the same instance, so kingdom(1) and kingdom(1.0) are one kingdom.

Circular dependencies

Two components that need each other can't both take the other in their initializer. Give one of them its dependency after the graph is built, with configure=:

class QuestAssembly(PilgrimAssembly):
    def knight(self) -> Knight:
        return self.shared(lambda: Knight(squire=self.squire()))

    def squire(self) -> Squire:
        return self.shared(
            lambda: Squire(),
            configure=lambda squire: setattr(squire, "master", self.knight()),
        )

Pilgrim runs every configure once each instance in the request has been made, in the order they were made, so self.knight() returns the knight that holds this squire. A cycle through initializers alone is an error that names the cycle, if any component in it is shared, weak_shared or object_graph:

PilgrimError: Circular dependency in initializers: shared(QuestAssembly.knight) -> shared(QuestAssembly.squire)
-> shared(QuestAssembly.knight). Break it with configure=.

A class can instead configure itself, by subclassing PilgrimConfigurable. Its configure(assembly) runs at the same point:

class Squire(PilgrimConfigurable):
    def configure(self, assembly):
        self.master = assembly.knight()

Closing

An assembly holds its shared components, and its weak_shared ones while they're alive. Closing it tears them down, newest first, so a component is closed before the ones it was made from, as Spring closes its singletons:

class DatabaseAssembly(PilgrimAssembly):
    def pool(self) -> Pool:
        return self.shared(lambda: Pool(url="postgresql://localhost/quests"))

    def repository(self) -> QuestRepository:
        return self.shared(lambda: QuestRepository(self.pool()))


async def main():
    async with DatabaseAssembly() as assembly:
        await assembly.repository().save(quest)
    # The repository, then the pool, are closed here, before the event loop ends.

A component's teardown is the first of these it has, so its class needn't know about Pilgrim:

closed with calls
await assembly.aclose(), async with aclose(), close() or shutdown(), and awaits what it returns
assembly.close(), with close() or shutdown()

__exit__ isn't called: called without __enter__, it releases a lock that isn't held, or runs a @contextmanager from the start. object_graph and unshared components aren't held, so closing doesn't close them, as Spring doesn't close its prototypes. Nor is a component that's a class.

destroy= tears a component down another way, and destroy=None leaves it open. init= runs as soon as the factory returns, before the instance is given to anything that depends on it, where configure= waits for the whole graph:

class QuestAssembly(PilgrimAssembly):
    def ledger(self) -> Ledger:
        return self.shared(lambda: Ledger(), init=lambda it: it.open(), destroy=lambda it: it.flush_and_close())

    def scroll(self) -> Scroll:
        return self.shared(lambda: Scroll(), destroy=None)  # closed by someone else
  • Async teardown. close() can't wait for one, so if any component's teardown is async (aclose(), an async def close(), or an async def destroy), it raises AsyncTeardownRequired before closing anything and says to use await aclose(). A sync destroy that returns a coroutine is found only when it is called: that component's teardown is reported as an AsyncTeardownRequired in the ExceptionGroup, and the others are closed. AsyncTeardownRequired is a PilgrimError, so except PilgrimError still catches it. An assembly whose teardown is async can say so, with AsyncPilgrimAssembly, below.
  • Errors. A failed teardown doesn't stop the others. Once every component is closed, their errors are raised together as an ExceptionGroup, each with a note naming its component.
  • A failed request keeps none of the components it made, so closing doesn't close them either. If a request can fail after it makes a pool, say, ask for the pool first, in a request of its own.
  • Closing twice does nothing.
  • After closing, the assembly makes no more components, and says so. Make a new assembly to start again.
  • Threads. A request in progress on another thread finishes, and its components are closed with the rest. One that comes after is refused.

Async assemblies

An assembly that holds something to close asynchronously, such as a database driver's pool, can subclass AsyncPilgrimAssembly. Its close() is aclose(), so await assembly.close() closes it as await assembly.aclose() does, and so does asyncio.run(assembly.close()), even if nothing async has been made yet:

class PoolAssembly(AsyncPilgrimAssembly):
    def pool(self) -> Pool:
        return self.shared(lambda: Pool(url="postgresql://localhost/quests"))


async def main():
    assembly = PoolAssembly()
    await assembly.pool().connect()
    ...
    await assembly.close()  # or await assembly.aclose(), or async with

Await what close() returns: left unawaited, it closes nothing, and Python warns that a coroutine was never awaited. mypy and pyright flag an unawaited close() on a variable of the subclass's type, but not on one typed PilgrimAssembly, whose close() returns None: there, call await aclose(), which is async on every assembly. with raises AsyncTeardownRequired on entering, before its body runs, since leaving it can't wait for the teardown. A plain PilgrimAssembly's close() is unchanged: it closes in sync and returns None.

shut down with PilgrimAssembly, sync teardowns PilgrimAssembly, an async teardown AsyncPilgrimAssembly
await aclose(), async with closes closes closes
close(), with closes AsyncTeardownRequired close() returns a coroutine to await; with refuses
await close(), asyncio.run(close()) closes, then await raises TypeError, and asyncio.run() ValueError (TypeError from Python 3.14), as close() returns None AsyncTeardownRequired closes
an owner's or holder's close() closes AsyncTeardownRequired closes if nothing async in it was made and it doesn't override aclose(), or else AsyncTeardownRequired
an owner's or holder's await aclose() closes closes closes

Nested assemblies

An assembly that asks AssemblyHolder for another, which the holder hasn't made yet, owns it, and closes it after its own components. That's the case for QuestAssembly under More than one assembly, which asks for MapAssembly in its initializer: closing the quests closes the maps. The holder then forgets the closed assembly, so the next AssemblyHolder.shared(MapAssembly) makes a new one.

An assembly that something else asked for first, such as an Assembled attribute or a component's own __init__, is owned by the holder. AssemblyHolder.close(), or await AssemblyHolder.aclose(), closes every assembly it holds, newest first, and leaves one that another owns to its owner, so each is closed before the ones it asked for:

@asynccontextmanager
async def lifespan(app: FastAPI):
    yield
    await AssemblyHolder.aclose()

An assembly that's a component of another, made with self.shared(MapAssembly), is closed with the rest of its components.

An assembly that another closes, as a component or as one it owns, or that AssemblyHolder closes, is closed through its own lifecycle, not its close(): await aclose() awaits its aclose(), and a sync close() closes it as PilgrimAssembly.close() does. So an AsyncPilgrimAssembly, or one that overrides close() as async def, is closed in sync when nothing async in it has been made; if something has, or an AsyncPilgrimAssembly overrides aclose(), the sync close() raises AsyncTeardownRequired before closing anything. A destroy= given for the component is used instead, as for any other.

So an override of an assembly's close() runs only when that assembly is closed itself, not when another closes it, and an override of its aclose() runs when it, its owner or its holder is closed with await aclose(). Before 0.4.0, a sync close called a held or owned assembly's own close(). To flush or log whenever an assembly closes, however it is closed, make that a shared component of it with a close() or a destroy=.

Injecting into top-level classes

Most of your classes are made by the assembly. A few, such as an application's entry point or a web framework's handlers, are made by something else. They can declare what they need with Assembled.

The assembly says what it can inject in make_bindings:

class QuestAssembly(PilgrimAssembly):
    def make_bindings(self):
        self.make_injectable(self.knight, by_type=Knight)
        self.make_injectable(self.quest, by_type=Quest)
        self.make_injectable(self.damsel_quest, by_key="damsel_quest")
    ...

A class asks for them by the annotated type, or by key:

from pipilgrim import Assembled, AssemblyHolder


class Tavern:
    knight: Knight = Assembled()
    quest: Quest = Assembled(key="damsel_quest")
    map: Map = Assembled(assembly=MapAssembly)  # from an assembly other than the default


AssemblyHolder.default_assembly_type = QuestAssembly  # once, at start-up

Tavern().knight
  • An attribute is resolved on first access, and each object keeps what it got. Whether two objects share it is up to the component's lifecycle.
  • Optional[Knight] and Knight | None inject as Knight.
  • An assembly is always injectable as its own type: assembly: QuestAssembly = Assembled().
  • A missing binding is an error that names the attribute and the assembly.

More than one assembly

AssemblyHolder.shared(SomeAssembly) returns the application's one instance of an assembly, so components are shared across the application. An assembly can use another's components directly, or make them injectable through itself:

class QuestAssembly(PilgrimAssembly):
    def __init__(self):
        self.maps = AssemblyHolder.shared(MapAssembly)
        super().__init__()

    def make_bindings(self):
        self.import_bindings(self.maps)  # Assembled can inject MapAssembly's components through this one

    def quest(self) -> Quest:
        return self.shared(lambda: HolyGrailQuest(map=self.maps.map()))

Where assemblies live

Give an assembly a module of its own, named for its job: quests/assembly.py. A larger application can have one for each package, quests/assembly.py and maps/assembly.py, joined with AssemblyHolder.shared as above.

A package's __init__.py may re-export its assembly, but shouldn't define it:

  • Python runs __init__.py whenever anything in the package is imported. Importing quests.knight runs quests/__init__.py first, so an assembly defined there imports every class it wires, on every import from the package.
  • That makes circular imports. If a module in the package imports from the package itself, __init__.py is part-way through importing it, and the name isn't there yet.
  • Readers expect __init__.py to say what a package exports, not how it is wired.

Resources

FileResource provides the text of a file, relative to a directory:

import os

from pipilgrim import FileResource


class QuestAssembly(PilgrimAssembly):
    def quest(self) -> Quest:
        return self.shared(lambda: HolyGrailQuest(
            description=FileResource(os.path.dirname(__file__), "quest.txt").provide()
        ))

InstanceProvider is the protocol for things like this: any object with a provide() method counts as one, so you can write your own and call them the same way.

Testing

  • Unit tests needn't involve Pilgrim: your classes take their collaborators in their initializers, so pass fakes.

  • To swap one component in an integration test, subclass the assembly and override that method:

    class TestQuestAssembly(QuestAssembly):
        def map(self) -> Map:
            return self.shared(lambda: FakeMap())
    

    An override can build on the method it replaces. Take super().map before the lambda, since super() with no arguments doesn't work inside one:

    class TestQuestAssembly(QuestAssembly):
        def map(self) -> Map:
            real = super().map
            return self.shared(lambda: RecordingMap(real()))
    
  • AssemblyHolder.reset() forgets every assembly and the default, so each test can start clean. It doesn't close them: call AssemblyHolder.close() first if they hold resources.

Threads and asyncio

Resolving is thread-safe. One thread at a time builds a graph from an assembly, and a shared component is made once however many threads ask for it at once. A shared or weak_shared component that already exists is returned without taking the lock. Each assembly has its own lock, so one assembly may call into another, as QuestAssembly calls MapAssembly above, but not back: two threads could each hold one lock and wait for the other.

Factories are plain functions, so a resolve never awaits and asyncio tasks can't interleave inside one. Make components that need async set-up, such as a connection pool, in the assembly, and open them at start-up:

assembly = AppAssembly()
await assembly.database().connect()
...
await assembly.aclose()  # closes the database, with its aclose() or close()

How it works

A lifecycle names the component by the method that called it, read from the caller's frame, along with that method's arguments. So call the lifecycle directly from the assembly method, as every example here does, and not from a helper that the method calls: the helper's name would be used instead. And leave the arguments as they were passed until then, since their values at that point are what name the instance.

Reading the frame costs well under a microsecond, where inspect.stack() reads every frame and its source file. Resolving a shared component that already exists takes about 0.3 µs, and making an unshared one about 1 µs, on an Apple M2 Max.

License

MIT. Copyright (c) 2024 Liberation Data.

pi-pilgrim follows Pilgrim, which is based on FieryCrucible by jkolb.

Metadata

Release files for pi-pilgrim 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pi-pilgrim 0.4.0
File Size Uploaded Provenance Actions
pi_pilgrim-0.4.0.tar.gz 23.7 kB With provenance Details for pi_pilgrim-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pi-pilgrim 0.4.0
File Interpreter ABI Platform Actions
pi_pilgrim-0.4.0-py3-none-any.whl Python 3 none any Details for pi_pilgrim-0.4.0-py3-none-any.whl

Total release size: 45.2 kB

Release files / pi_pilgrim-0.4.0.tar.gz

Download URL pi_pilgrim-0.4.0.tar.gz
Size 23.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e308a9e1e103ca48574ed86a48cf07e8bbaa5eb7c035382ff2b3dc6b7c85577d
BLAKE2b-256 checksum
How to use checksums
4fcf4f387a921d833cdcb87e264c1a6be5c11b194a901e665cb5284874dc900c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / pi_pilgrim-0.4.0-py3-none-any.whl

Download URL pi_pilgrim-0.4.0-py3-none-any.whl
Size 21.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de5f5d0010d56e7303a45e795a7bba4c505f226b6f7888db0600377d16c5a2a0
BLAKE2b-256 checksum
How to use checksums
0877212b07020427ac893273ffc71aa32ca1aab42da0556a0895ee75e2f82bd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for pi-pilgrim 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pi_pilgrim-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page