lightbulb.di.registry

class Registry[source]

A dependency registry containing information about how to create the registered dependencies.

You can use in to check if a dependency has been registered to this registry.

Example

>>> registry = lightbulb.di.Registry()
>>> object in registry
False
>>> registry.register_value(object, object())
>>> object in registry
True

Note

When containers are created for a registry, the registry is frozen to prevent additional dependencies being registered. The registry is unfrozen once all containers providing from a registry have been closed.

register_factory(typ: type[T], factory: Callable[..., types.MaybeAwaitable[T]], *, teardown: Callable[[T], types.MaybeAwaitable[None]] | None = None) None[source]

Registers a factory for creating a dependency.

Parameters:
  • typ – The type to register the dependency as.

  • factory – The factory used to create the dependency. A factory method may take any number of parameters. The parameters will all attempt to be dependency-injected when creating the dependency. Any default parameter values will be ignored.

  • teardown – The teardown function to be called when the container is closed. Teardown functions must take exactly one argument - the dependency that is being torn down. Defaults to None.

Returns:

None

Raises:
register_value(typ: type[T], value: T, *, teardown: Callable[[T], types.MaybeAwaitable[None]] | None = None) None[source]

Registers a pre-existing value as a dependency.

Parameters:
  • typ – The type to register the dependency as.

  • value – The value to use for the dependency.

  • teardown – The teardown function to be called when the container is closed. Teardown functions must take exactly one argument - the dependency that is being torn down. Defaults to None.

Returns:

None

Raises:

RegistryFrozenException – If the registry is frozen.