On this page

Spellframes and categories๏ƒ

Beginner. The organizing half of every address.

A spellframe is the category half of the Address Law. It is a plain string when you just want grouping, or a Protocol when you want a contract. Nothing else is a spellframe: a concrete class is refused at bind() with a TypeError that names the two forms (pass spellframe="Name" to group under its name, or declare the shape as a typing.Protocol).

# strings โ€” organize by role
book.bind(spell=UsersRepo, existence="unique",
          spellframe="storage", binding_name="users")
book.bind(spell=UsersApi,  existence="unique",
          spellframe="web",     binding_name="users")

repo = conduit.meld(spellframe="storage", binding_name="users")
api  = conduit.meld(spellframe="web",     binding_name="users")

This is an excerpt: define UsersRepo and UsersApi, bind them before conjure, then resolve each full address. The result is a repository and an API object in separate categories, even though both bindings are named users.

Same binding_name, two categories, two distinct addresses โ€” names never collide across spellframes. A string frame is only a label: it groups, it is addressable, and it never makes its members providers of anything. A Protocol frame is a label and a contract: bind() checks the spell against the Protocol's own public members, records the Protocol on the binding, and a parameter annotated with that Protocol resolves exactly its recorded implementers โ€” which is what collection DI gathers:

book.bind(spell=EmailHandler, existence="unique",
          spellframe=Handler, binding_name="email")

A spellframe is the spell's interface: think of it as a dict keyed by binding_name. Bound under a frame, a spell is reached through the frame โ€” a Handler annotation, list[Handler], SpellMap(spellframe=Handler, binding_name="email") or meld(spellframe=Handler, binding_name="email") โ€” and never by its own class: handler: EmailHandler and meld(EmailHandler) do not find this EmailHandler, just as a service registered under an interface hides its own type in Autofac. Like a dict, meld(spellframe=Handler) with no binding_name reads the default slot (a KeyError when every member is named), while a Handler annotation selects the frame's single implementation and refuses several with AMBIGUOUS_PROVIDER.

Runnable examples๏ƒ

All beginner examples ยท Level contents ยท Full contents

Canonical page source