Skip to content

Container

The registration API (ServiceCollection), the built container (ServiceProvider), and the per-scope view (ServiceScope).

See Registration, Resolution, and Lifetimes and scopes for the narrative versions.

ServiceCollection

ServiceCollection

ServiceCollection()

Collects service registrations before building a ServiceProvider.

Source code in packages/depi-core/depi/services.py
def __init__(self):
    self._container: dict[type, DependencyRegistration] = {}

get_type_dependencies

get_type_dependencies(_type: type) -> list[ConstructorDependency]

Inspect init signature to auto-discover constructor dependencies.

Source code in packages/depi-core/depi/services.py
def get_type_dependencies(self, _type: type) -> list[ConstructorDependency]:
    """
    Inspect __init__ signature to auto-discover constructor dependencies.
    """
    params = get_signature(_type).parameters
    deps = []
    for name, param in params.items():
        if param.annotation == inspect.Parameter.empty:
            raise MissingAnnotationError(
                f"Missing type annotation for parameter '{name}' in {_type.__name__}")
        deps.append(ConstructorDependency(name=name, _type=param.annotation))
    return deps

add

add(dependency_type: type, implementation_type: Optional[type] = None, **kwargs)

Shorthand to register a service (default: Transient).

Source code in packages/depi-core/depi/services.py
def add(
    self,
    dependency_type: type,
    implementation_type: Optional[type] = None,
    **kwargs
):
    """
    Shorthand to register a service (default: Transient).
    """
    kwargs.setdefault('lifetime', Lifetime.Transient)
    self._register_dependency(dependency_type, implementation_type, **kwargs)

add_singleton

add_singleton(dependency_type: type, implementation_type: Optional[type] = None, instance: Any = None, factory: Callable = None, eager: bool = False) -> None

Register a singleton service.

eager: If True, construct the singleton during build()/build_async() instead of lazily on first resolve().

Source code in packages/depi-core/depi/services.py
def add_singleton(
    self,
    dependency_type: type,
    implementation_type: Optional[type] = None,
    instance: Any = None,
    factory: Callable = None,
    eager: bool = False
) -> None:
    """
    Register a singleton service.

    `eager`: If True, construct the singleton during build()/build_async()
    instead of lazily on first resolve().
    """
    self._register_dependency(
        dependency_type=dependency_type,
        implementation_type=implementation_type,
        lifetime=Lifetime.Singleton,
        instance=instance,
        factory=factory,
        eager=eager
    )

add_transient

add_transient(dependency_type: type, implementation_type: Optional[type] = None, factory: Callable = None) -> None

Register a transient service.

Source code in packages/depi-core/depi/services.py
def add_transient(
    self,
    dependency_type: type,
    implementation_type: Optional[type] = None,
    factory: Callable = None
) -> None:
    """Register a transient service."""
    self._register_dependency(
        dependency_type=dependency_type,
        implementation_type=implementation_type,
        lifetime=Lifetime.Transient,
        factory=factory
    )

add_scoped

add_scoped(dependency_type: type, implementation_type: Optional[type] = None, factory: Callable = None) -> None

Register a scoped service.

Source code in packages/depi-core/depi/services.py
def add_scoped(
    self,
    dependency_type: type,
    implementation_type: Optional[type] = None,
    factory: Callable = None
) -> None:
    """Register a scoped service."""
    self._register_dependency(
        dependency_type=dependency_type,
        implementation_type=implementation_type,
        lifetime=Lifetime.Scoped,
        factory=factory
    )

register_many

register_many(types: list[type], lifetime: str = Transient)

Bulk-register multiple types with the same lifetime.

Source code in packages/depi-core/depi/services.py
def register_many(self, types: list[type], lifetime: str = Lifetime.Transient):
    """
    Bulk-register multiple types with the same lifetime.
    """
    for t in types:
        self._register_dependency(
            dependency_type=t,
            implementation_type=None,
            lifetime=lifetime
        )

get_container

get_container() -> dict[type, DependencyRegistration]

Expose raw registration dictionary.

Source code in packages/depi-core/depi/services.py
def get_container(self) -> dict[type, DependencyRegistration]:
    """Expose raw registration dictionary."""
    return self._container

build_provider

build_provider(eager_all: bool = False) -> ServiceProvider

Finalize registrations and return a built ServiceProvider.

eager_all: If True, mark every singleton registration as eager so all singletons are constructed during build() (pre-lazy behavior), instead of only those explicitly registered with eager=True.

Source code in packages/depi-core/depi/services.py
def build_provider(self, eager_all: bool = False) -> 'ServiceProvider':
    """
    Finalize registrations and return a built ServiceProvider.

    `eager_all`: If True, mark every singleton registration as eager so
    all singletons are constructed during build() (pre-lazy behavior),
    instead of only those explicitly registered with eager=True.
    """
    if eager_all:
        for reg in self._container.values():
            if reg.lifetime == Lifetime.Singleton:
                reg.eager = True
    provider = ServiceProvider(self)
    provider.build()
    return provider

ServiceProvider

ServiceProvider

ServiceProvider(service_collection: ServiceCollection)

Resolves and caches instances according to registration metadata.

Key methods
  • resolve(type) → sync instance
  • resolve_async(type)→ async instance
  • build() / build_async() → pre-instantiate singletons
  • create_scope() → new ServiceScope for scoped lifetimes
Source code in packages/depi-core/depi/services.py
def __init__(self, service_collection: ServiceCollection):
    self._service_collection = service_collection
    self._dependency_lookup = service_collection.get_container()
    self._dependencies = list(self._dependency_lookup.values())
    self._singleton_instances: dict[type, Any] = {}
    self._cache: dict = {}
    # RLock (not Lock) is required: singleton A's constructor may call
    # resolve(B) while this lock is held. RLock lets the same thread
    # re-enter, preventing deadlock during recursive singleton construction.
    self._cache_lock = RLock()
    # Per-type asyncio.Lock objects for coroutine-safe lazy singleton
    # creation. Lazily populated; the dict itself is protected by a small
    # sync Lock held only while reading or inserting lock objects.
    self._async_singleton_locks: dict[type, asyncio.Lock] = {}
    self._async_lock_dict_lock = Lock()
    self._initialize_provider()

resolve

resolve(_type: type) -> Any

Resolve a registered service synchronously. Optimized with fast path for singletons and minimal locking.

Source code in packages/depi-core/depi/services.py
def resolve(self, _type: type) -> Any:
    """
    Resolve a registered service synchronously.
    Optimized with fast path for singletons and minimal locking.
    """
    reg = self._get_registered_dependency(_type)
    lifetime = reg.lifetime

    if lifetime == Lifetime.Singleton:
        # Use registration.dependency_type as the consistent cache key.
        dep_type = reg.dependency_type
        cache = self._singleton_instances

        instance = cache.get(dep_type)
        if instance is not None:
            return instance

        # RLock allows the same thread to re-enter while constructing a
        # singleton whose constructor itself calls resolve() for another
        # singleton (e.g. A.__init__ calls resolve(B)).
        with self._cache_lock:
            instance = cache.get(dep_type)
            if instance is not None:
                return instance

            if reg.factory:
                instance = reg.factory(self)
                if inspect.isawaitable(instance):
                    _reject_awaitable(_type, instance)
            elif reg.instance is not None:
                instance = reg.instance
            else:
                instance = reg.activate(self)

            cache[dep_type] = instance
            return instance

    elif lifetime == Lifetime.Transient:
        if reg.factory:
            instance = reg.factory(self)
            if inspect.isawaitable(instance):
                _reject_awaitable(_type, instance)
            return instance
        return reg.activate(self)

    elif lifetime == Lifetime.Scoped:
        raise ScopeRequiredError("Scoped resolution requires a scope. Call provider.create_scope().")

    raise UnknownLifetimeError(f"Unknown lifetime: {lifetime}")

resolve_async async

resolve_async(_type: type) -> Any

Resolve a registered service asynchronously. Optimized with fast path for singletons and minimal locking.

Source code in packages/depi-core/depi/services.py
async def resolve_async(self, _type: type) -> Any:
    """
    Resolve a registered service asynchronously.
    Optimized with fast path for singletons and minimal locking.
    """
    reg = self._get_registered_dependency(_type)

    if reg.lifetime == Lifetime.Singleton:
        # Use registration.dependency_type as the consistent cache key.
        dep_type = reg.dependency_type

        # Fast path: lock-free cache check (covers 99% of singleton calls)
        instance = self._singleton_instances.get(dep_type)
        if instance is not None:
            return instance

        # Per-type asyncio.Lock is required for coroutine-safe lazy singleton
        # construction. A single global async lock would deadlock when singleton
        # A's constructor awaits resolve_async(B) — both would compete for the
        # same lock. Per-type locks let independent singletons be built
        # concurrently without blocking each other.
        #
        # The dict lock is held only long enough to read or create the asyncio.Lock
        # object; it is never held across an await.
        with self._async_lock_dict_lock:
            lock = self._async_singleton_locks.get(dep_type)
            if lock is None:
                lock = asyncio.Lock()
                self._async_singleton_locks[dep_type] = lock

        async with lock:
            # Double-check after acquiring the per-type async lock.
            instance = self._singleton_instances.get(dep_type)
            if instance is not None:
                return instance

            if reg.factory:
                inst = reg.factory(self)
                if inspect.isawaitable(inst):
                    inst = await inst
                instance = inst
            elif reg.instance is not None:
                # Pre-built during build() phase
                instance = reg.instance
            else:
                # Lazy singleton via async constructor injection
                instance = await reg.activate_async(self)

            self._singleton_instances[dep_type] = instance
            return instance

    elif reg.lifetime == Lifetime.Transient:
        if reg.factory:
            inst = reg.factory(self)
            return await inst if inspect.isawaitable(inst) else inst
        else:
            return await reg.activate_async(self)

    elif reg.lifetime == Lifetime.Scoped:
        raise ScopeRequiredError("Scoped resolution requires a scope. Call provider.create_scope().")

    raise UnknownLifetimeError(f"Unknown lifetime: {reg.lifetime}")

is_registered

is_registered(_type: type) -> bool

Return True if _type has a registration.

Integrations use this to decide whether an annotated view-function parameter is something depi owns, or something the web framework will supply (a path converter, a query argument).

Source code in packages/depi-core/depi/services.py
def is_registered(self, _type: type) -> bool:
    """
    Return True if ``_type`` has a registration.

    Integrations use this to decide whether an annotated view-function
    parameter is something depi owns, or something the web framework will
    supply (a path converter, a query argument).
    """
    return _type in self._dependency_lookup

build

build() -> ServiceProvider

Validate and order every singleton registration, but only construct factories and singletons explicitly marked eager=True. Other singletons are validated/ordered here and constructed lazily on first resolve().

Source code in packages/depi-core/depi/services.py
def build(self) -> 'ServiceProvider':
    """
    Validate and order every singleton registration, but only construct
    factories and singletons explicitly marked eager=True. Other
    singletons are validated/ordered here and constructed lazily on
    first resolve().
    """
    # Validate dependencies before building
    self._validate_dependencies()
    # whole-graph cycle check across every lifetime (lenient on missing deps)
    self._walk_dependencies(self._dependencies, strict=False, collect_order=False)

    to_build = [d for d in self._dependencies if d.lifetime == Lifetime.Singleton]
    # strict ordered pass validates/orders all singletons, even lazy ones
    sorted_deps = self._walk_dependencies(to_build, strict=True, collect_order=True)
    for reg in sorted_deps:
        if not (reg.factory or reg.eager):
            continue
        if reg.instance is None:
            if reg.factory:
                inst = reg.factory(self)
                # support coroutine factories
                if asyncio.iscoroutine(inst):
                    try:
                        # Probe only: raises RuntimeError when there is no
                        # running loop, which the except branch below handles.
                        asyncio.get_running_loop()
                        # run in separate thread if already in event loop
                        import concurrent.futures
                        with concurrent.futures.ThreadPoolExecutor() as ex:
                            inst = ex.submit(asyncio.run, inst).result()
                    except RuntimeError:
                        inst = asyncio.run(inst)
            else:
                inst = reg.activate(self)
            reg.instance = inst
        with self._cache_lock:
            self._singleton_instances[reg.dependency_type] = reg.instance
    return self

build_async async

build_async() -> ServiceProvider

Async variant of build(): validates/orders every singleton, but only constructs factories and singletons explicitly marked eager=True.

Source code in packages/depi-core/depi/services.py
async def build_async(self) -> 'ServiceProvider':
    """
    Async variant of build(): validates/orders every singleton, but only
    constructs factories and singletons explicitly marked eager=True.
    """
    # Validate dependencies before building
    self._validate_dependencies()
    # whole-graph cycle check across every lifetime (lenient on missing deps)
    self._walk_dependencies(self._dependencies, strict=False, collect_order=False)

    to_build = [d for d in self._dependencies if d.lifetime == Lifetime.Singleton]
    # strict ordered pass validates/orders all singletons, even lazy ones
    sorted_deps = self._walk_dependencies(to_build, strict=True, collect_order=True)
    for reg in sorted_deps:
        if not (reg.factory or reg.eager):
            continue
        if reg.instance is None:
            if reg.factory:
                inst = reg.factory(self)
                if asyncio.iscoroutine(inst):
                    inst = await inst
            else:
                inst = await reg.activate_async(self)
            reg.instance = inst
        with self._cache_lock:
            self._singleton_instances[reg.dependency_type] = reg.instance
    return self

create_scope

create_scope() -> ServiceScope

Begin a new scoped lifetime context.

Source code in packages/depi-core/depi/services.py
def create_scope(self) -> 'ServiceScope':
    """Begin a new scoped lifetime context."""
    return ServiceScope(self)

ServiceScope

ServiceScope

ServiceScope(provider: ServiceProvider)

Provides scoped resolution: Singleton → cascades to provider, Transient → new each call, Scoped → one per scope instance.

Source code in packages/depi-core/depi/services.py
def __init__(self, provider: ServiceProvider):
    self._provider = provider
    self._scoped_instances: dict[type, Any] = {}
    self._dependency_lookup = provider._dependency_lookup
    self._cache: dict = {}
    self._cache_lock = Lock()

resolve

resolve(_type: type) -> Any

Resolve _type against this scope.

Singletons cascade to the root provider; scoped services return the one instance held by this scope (constructing it on first request); transients are built fresh on every call.

Raises:

Source code in packages/depi-core/depi/services.py
def resolve(self, _type: type) -> Any:
    """
    Resolve ``_type`` against this scope.

    Singletons cascade to the root provider; scoped services return the
    one instance held by this scope (constructing it on first request);
    transients are built fresh on every call.

    Raises:
        UnregisteredDependencyError: ``_type`` has no registration.
        AsyncFactoryError: ``_type`` has an async factory; use
            ``resolve_async``.
    """
    provider = self._provider
    reg = provider._get_registered_dependency(_type)
    life = reg.lifetime

    # Singleton always via root provider
    if life == Lifetime.Singleton:
        return provider.resolve(_type)

    insts = self._scoped_instances

    # Scoped: return cached if present
    if life == Lifetime.Scoped:
        inst = insts.get(_type)
        if inst is not None:
            return inst

    # Transient or new Scoped instance
    factory = reg.factory
    if factory:
        inst = factory(self)
        # Same guard ServiceProvider.resolve applies. Without it an async
        # factory resolved through a scope returns an un-awaited coroutine,
        # which then fails far away from its cause -- and scopes are what
        # every web integration resolves through.
        if inspect.isawaitable(inst):
            _reject_awaitable(_type, inst)
    else:
        # your precompiled sync resolver
        inst = reg._resolver_fn(self)

    # Cache scoped
    if life is Lifetime.Scoped:
        insts[_type] = inst

    return inst

resolve_async async

resolve_async(_type: type) -> Any

Async variant of resolve.

Awaits async factories and async constructor dependencies. Scoped instances are cached for the life of the scope, so a repeated resolution returns the same object without re-awaiting.

Raises:

Source code in packages/depi-core/depi/services.py
async def resolve_async(self, _type: type) -> Any:
    """
    Async variant of ``resolve``.

    Awaits async factories and async constructor dependencies. Scoped
    instances are cached for the life of the scope, so a repeated
    resolution returns the same object without re-awaiting.

    Raises:
        UnregisteredDependencyError: ``_type`` has no registration.
    """
    provider = self._provider
    reg = provider._get_registered_dependency(_type)
    life = reg.lifetime

    # Singleton via root provider
    if life is Lifetime.Singleton:
        return await provider.resolve_async(_type)

    insts = self._scoped_instances

    # Scoped: return cached if present
    if life is Lifetime.Scoped:
        inst = insts.get(_type)
        if inst is not None:
            return inst

    # Transient or new Scoped instance
    factory = reg.factory
    if factory:
        inst = factory(self)
        if asyncio.iscoroutine(inst):
            inst = await inst
    else:
        # fall back to the generic async activation
        inst = await reg.activate_async(self)

    # Cache scoped
    if life is Lifetime.Scoped:
        insts[_type] = inst

    return inst

is_registered

is_registered(_type: type) -> bool

Return True if _type has a registration. See ServiceProvider.is_registered.

Source code in packages/depi-core/depi/services.py
def is_registered(self, _type: type) -> bool:
    """Return True if ``_type`` has a registration. See ServiceProvider.is_registered."""
    return _type in self._dependency_lookup

dispose

dispose() -> None

Clear scoped instances and internal cache. Call dispose on disposable instances.

Source code in packages/depi-core/depi/services.py
def dispose(self) -> None:
    """
    Clear scoped instances and internal cache. Call dispose on disposable instances.
    """
    # Dispose of any disposable instances
    for instance in self._scoped_instances.values():
        if hasattr(instance, 'dispose') and callable(getattr(instance, 'dispose')):
            try:
                instance.dispose()
            except Exception as e:
                logger.warning(f"Error disposing scoped instance: {e}")

    self._scoped_instances.clear()
    self._cache.clear()

Lifetime

Lifetime

Supported lifetimes for registered dependencies.

Registration records

These describe a registration after it has been parsed. Application code rarely touches them directly; they are public because factories and diagnostics can read them.

ConstructorDependency

ConstructorDependency(name: str, _type: type)

Represents a single constructor parameter dependency.

Attributes:

  • name –

    Name of the parameter in the constructor.

  • dependency_type –

    The type annotation required.

Source code in packages/depi-core/depi/services.py
def __init__(self, name: str, _type: type):
    self.name = name
    self.dependency_type = _type

DependencyRegistration

DependencyRegistration(dependency_type: type, lifetime: str, implementation_type: Optional[type] = None, instance: Any = None, factory: Callable[[DependencyRegistration], Any] = None, eager: bool = False, constructor_params: list[ConstructorDependency] = None, resolver_fn: Optional[Callable] = None)

Holds metadata and factory logic for a single registered service.

Attributes:

  • dependency_type –

    The abstract/base type.

  • implementation_type –

    The concrete class to instantiate.

  • lifetime –

    Lifetime.Singleton, Transient, or Scoped.

  • instance –

    Stored instance for singletons.

  • factory –

    Optional callable(provider) → instance.

  • constructor_params –

    List of ConstructorDependency for auto-injection.

Source code in packages/depi-core/depi/services.py
def __init__(
    self,
    dependency_type: type,
    lifetime: str,
    implementation_type: Optional[type] = None,
    instance: Any = None,
    factory: Callable[['DependencyRegistration'], Any] = None,
    eager: bool = False,
    constructor_params: list[ConstructorDependency] = None,
    resolver_fn: Optional[Callable] = None
):
    self.dependency_type = dependency_type
    self.lifetime = lifetime
    self.implementation_type = implementation_type or dependency_type
    self.instance = instance
    self.factory = factory
    self.eager = eager
    self.constructor_params = constructor_params or []
    self._type_name = self.implementation_type.__name__
    self._resolver_fn = resolver_fn

activate

activate(provider=None) -> Any

Instantiate this service according to its lifetime and factory/constructor logic.

Source code in packages/depi-core/depi/services.py
def activate(
    self,
    provider=None
) -> Any:
    """
    Instantiate this service according to its lifetime and factory/constructor logic.
    """
    if not self.constructor_params:
        instance = self.implementation_type()
    else:
        kwargs = {param.name: provider.resolve(param.dependency_type) for param in self.constructor_params}
        instance = self.implementation_type(**kwargs)
    return instance

activate_async async

activate_async(provider=None) -> Any

Async variant of activate, supporting coroutine factories and constructors.

Source code in packages/depi-core/depi/services.py
async def activate_async(
    self,
    provider=None
) -> Any:
    """
    Async variant of activate, supporting coroutine factories and constructors.
    """
    if not self.constructor_params:
        return self.implementation_type()
    else:
        kwargs = {}
        for param in self.constructor_params:
            kwargs[param.name] = await provider.resolve_async(param.dependency_type)
        return self.implementation_type(**kwargs)