Module API¶
Module¶
@dataclass
class Module:
name: str
providers: list[Provider | type]
exports: list[type]
imports: list[Module | ForwardRef]
on_destroy: Callable[[Container], Awaitable[None]] | None = None
| Field | Description |
|---|---|
name |
Unique module identifier |
providers |
Types this module registers |
exports |
Types visible to importing modules |
imports |
Modules this module depends on |
on_destroy |
Async callback called on shutdown() with the module's container |
ApplicationContext¶
__init__(modules: list[Module], globals: list[Provider] | None = None)¶
Compose modules with boundary enforcement.
Boot-time validation:
- Every export must exist in providers (
ExportWithoutProviderError) - Every import must be in the modules list (
ModuleNotFoundError) - No circular direct imports (
CircularModuleError)
resolve(type_: type[T]) -> T¶
Resolve from the global container. No boundary checks.
resolve_within(module: Module, type_: type[T]) -> T¶
Resolve within a module's boundary.
Raises: ModuleBoundaryError if accessing a non-exported type from another module.
container: Container (property)¶
Access the underlying global container.
on_shutdown(hook: Callable) -> None¶
Register a shutdown hook.
async shutdown() -> None¶
- Run
on_destroyfor each module (reverse order) - Close managed instances (
__aexit__/aclose/close) - Run container shutdown hooks
create_scope() -> ScopedContainer¶
Create a scoped container from the global container.