API Reference¶
Container¶
The core dependency injection container.
Container()¶
Creates an empty container with no registered providers.
Container.from_module(mod) -> Container¶
Creates a container from a module hierarchy. Scans the module tree, registers all providers, and returns a fully configured Container.
container.injectable(token=None, scope=SINGLETON)¶
Decorator that registers a class or factory as a provider.
| Parameter | Type | Default | Description |
|---|---|---|---|
token |
Any \| None |
None |
Token to register under. Defaults to the class itself. |
scope |
str |
SINGLETON |
Lifetime scope. |
container.register(token, provider, scope=SINGLETON)¶
Explicitly bind a provider to a token at runtime.
container.resolve(token, id=None) -> T¶
Resolve a dependency by token, respecting its scope.
| Parameter | Type | Description |
|---|---|---|
token |
type[T] \| Any |
The type or key to resolve. |
id |
str \| None |
Request identifier for REQUEST scoped deps. |
Raises RuntimeError on circular dependency or unregistered token.
container.call(target, id=None, **kwargs)¶
Invoke a callable with dependencies resolved and injected automatically.
container.on_bootstrap(fn)¶
Register a hook to run during bootstrap().
container.on_shutdown(fn)¶
Register a hook to run during shutdown().
container.bootstrap()¶
Execute all registered bootstrap hooks in order.
container.shutdown()¶
Execute all shutdown hooks and clear internal caches.
Async Context Manager¶
The container supports async with for automatic lifecycle management:
async with Container.from_module(AppModule) as container:
# @on_bootstrap hooks ran
...
# @on_shutdown hooks ran
Module-Level Shortcuts¶
These are shortcuts that operate on a global Container instance. Ideal for simple apps:
| Function | Description |
|---|---|
injectable |
Decorator to register a provider on the global container. |
register |
Bind a provider to a token on the global container. |
resolve |
Resolve a dependency from the global container. |
instance() |
Create a new, independent Container. |
Provide¶
Dataclass for custom provider registration within modules.
| Attribute | Type | Default | Description |
|---|---|---|---|
token |
Any |
— | The token to bind. |
use_value |
Any |
None |
Binds a constant value. |
use_class |
Any |
None |
Binds a class to instantiate. |
use_factory |
Callable \| None |
None |
Binds a callable to invoke. |
scope |
str |
SINGLETON |
Lifetime scope. |
@module(providers=[
Provide("DB_URL", use_value="postgresql://localhost/mydb"),
Provide(Cache, use_class=RedisCache),
Provide(HttpClient, use_factory=create_http_client),
])
class InfraModule: ...
Inject¶
Marker used in Annotated type hints to specify a custom injection token.
module¶
Decorator that marks a class as a Module, and namespace for lifecycle decorators.
As a decorator¶
Marks a class as a Module, grouping providers and configuring imports/exports.
| Parameter | Type | Default | Description |
|---|---|---|---|
providers |
list[Any] |
[] |
Providers to register within this module. |
imports |
list[Any] |
[] |
Imported modules that export providers. |
exports |
list[Any] |
[] |
Providers to export to importing modules. |
globals |
bool |
False |
If True, exports are available globally. |
module.on_bootstrap(fn=None, /, container=None)¶
Decorator to register a bootstrap hook. If container is omitted, registers on the global container.
# On a method (uses the container that resolves the class)
@module.on_bootstrap
async def connect(self): ...
# On a standalone function with specific container
@module.on_bootstrap(container=c)
async def startup(db: Database): ...
module.on_shutdown(fn=None, /, container=None)¶
Decorator to register a shutdown hook. If container is omitted, registers on the global container.
@module.on_shutdown
async def disconnect(self): ...
@module.on_shutdown(container=c)
async def teardown(): ...
DynamicModule¶
TypedDict returned by static methods to configure dynamic modules.
| Key | Type | Description |
|---|---|---|
module |
Any |
The module class being configured. |
providers |
list[Any] |
Additional providers to register. |
imports |
list[Any] |
Additional modules to import. |
exports |
list[Any] |
Additional providers to export. |
globals |
bool |
Whether exports are globally available. |
Scope Constants¶
| Constant | Value | Description |
|---|---|---|
SINGLETON |
"singleton" |
Single instance shared across all resolutions. |
TRANSIENT |
"transient" |
New instance on every resolve() call. |
REQUEST |
"request" |
One instance per request ID. |