Skip to content

Generated API Documentation#

diy #

diy is a modern dependency injection container that reads Pythons native type annotations, so you don't have to clutter your code with Annotated or other library specific markers.

At the root level, this package exports only the two default implementations of the specification and the container protocools. More specialized components must be imported from their respective modules.

Container #

Container(spec: SpecificationProtocol | None = None)

The default and most flexible implementation of a Container.

It is a specification and container at the same time, so you can teach it how to construct types, as well as resolve ones.

While this seems simple, since you only need to construct one "thing", it may be hard to trace down when a builder was registered, and by whom. This can theoretically happen at any time.

add #

add(builder: Callable[..., Any] | type[T], name: str | None = None) -> Callable[..., Any] | None

get #

get(abstract: type[T], name: str | None = None) -> Callable[..., T] | None

types #

types() -> set[type[Any]]

TODO:

resolve #

resolve(abstract: type[T]) -> T

Retrieves an instance of the given type from the container.

Depending on how the container was configured, this could return a shared instance (also referred to as a singleton), construct a fresh one, or even fail, since the container does not know the type or does not have enough information how to build it.

call #

call(function: Callable[..., R]) -> R

Calls the given function after resolving all required parameters from the container.

Specification #

Specification()

Registers functions that construct certain types or parameters.

builders instance-attribute #

builders: Builders = Builders()

Add and retrieve builder functions for types.

partials instance-attribute #

partials: Partials = Partials()

Add and retrieve builder functions for constructor paramters of types.

add #

add(builder: Callable[..., Any] | type[T], name: str | None = None) -> Callable[..., T] | Callable[..., Any] | None

get #

get(abstract: type[Any], name: str | None = None) -> Callable[..., Any] | None

types #

types() -> set[type[Any]]

TODO: