Types¶
Lifetime¶
- class diwire.Lifetime(*values)[source]¶
Define cache behavior for provider results.
- SCOPED = 2¶
Cache per declared scope owner until that scope exits.
For registrations declared on the container root scope, this behaves like a singleton for the container lifetime. Cleanup-enabled providers are released when the owning scope/resolver is closed.
- TRANSIENT = 1¶
Disable caching and build a new value for every resolution call.
LockMode¶
- class diwire.LockMode(*values)[source]¶
Select locking behavior for cached provider resolution.
Use these values for provider-level
lock_modeor container-level defaults. Container methods also accept"auto"at configuration time: DIWire maps"auto"to async locks when a graph requires async resolution and to thread locks for sync-only graphs.In mixed workloads, prefer forcing
THREADfor high-throughput sync cached paths when you do not want auto mode to pick async locks.- ASYNC = 'async'¶
Guard cached values with
asyncio.Lockfor async resolution paths.
- NONE = 'none'¶
Disable locking around cache reads/writes for this provider.
- THREAD = 'thread'¶
Guard cached values with
threading.Lockin synchronous paths.
ResolverProtocol¶
- class diwire.ResolverProtocol(*args, **kwargs)[source]¶
Protocol for a dependency resolver.
- async aclose(exc_type=None, exc_value=None, traceback=None)[source]¶
Asynchronously close the resolver by forwarding to
__aexit__.- Parameters:
exc_type – Exception type forwarded to close hooks.
exc_value – Exception instance forwarded to close hooks.
traceback – Traceback forwarded to close hooks.
- async aresolve(dependency)[source]¶
Resolve the given dependency asynchronously and return its instance.
- Parameters:
dependency – Dependency key to resolve.
- close(exc_type=None, exc_value=None, traceback=None)[source]¶
Close the resolver by forwarding to
__exit__with the given exception context.- Parameters:
exc_type – Exception type forwarded to close hooks.
exc_value – Exception instance forwarded to close hooks.
traceback – Traceback forwarded to close hooks.
Scope¶
- class diwire.BaseScope(*args, **_kwargs)[source]¶
Represent a numeric DI scope level with transition metadata.
A scope behaves like an
intso comparisons and ordering are based onlevel.skippablemarks helper scopes that can be skipped by default transitions whenenter_scope(None)chooses the next non-skippable scope.
- diwire.Scope = Scopes(skippable=(Scope.SESSION(2, skippable=True),), APP=Scope.APP(1, skippable=False), SESSION=Scope.SESSION(2, skippable=True), REQUEST=Scope.REQUEST(3, skippable=False), ACTION=Scope.ACTION(4, skippable=False), STEP=Scope.STEP(5, skippable=False))¶
Define the built-in DI scope ladder ordered by
level.Levels increase with nesting depth. Calling
enter_scope()with no explicit target transitions to the next deeper non-skippable scope.Examples
with container.enter_scope(Scope.REQUEST) as request_resolver: service = request_resolver.resolve(Service)
Injected¶
Component¶
- class diwire.Component(value)[source]¶
Differentiate multiple providers for the same base type.
Attach
Componentmetadata totyping.Annotatedso DIWire treats each annotated key as distinct at runtime.Examples
from typing import Annotated, TypeAlias class Database: ... ReplicaDb: TypeAlias = Annotated[Database, Component("replica")] PrimaryDb: TypeAlias = Annotated[Database, Component("primary")]
All¶
- class diwire.All[source]¶
Resolve all implementations registered for a base dependency key.
At runtime
All[T]resolves toAnnotated[T, AllMarker(dependency_key=T)]and is detected by the resolver dispatch to collect the plain registration forT(if any) plus all component-qualified registrations keyed asAnnotated[T, Component(...)].Notes
If you pass an
Annotated[...]token (for exampleAll[Annotated[T, Component('x')]]), DIWire strips it to the base typeTand produces the same token asAll[T]. Prefer usingAll[BaseType].