smart_injector

smart_injector.create_container(configure: Optional[Callable[[smart_injector.config.user.Config], None]] = None, default_lifetime=<Lifetime.TRANSIENT: 1>, dependencies: Optional[List[object]] = None) → smart_injector.container.container.StaticContainer[source]

Use this function to create a DI container.

Parameters:
  • configure
  • default_lifetime
  • dependencies
Returns:

class smart_injector.StaticContainer(resolver: smart_injector.resolver.resolver.Resolver)[source]

DI Container. Used by the user to get instances of types.

To get your own container. Create a new class inherited from this class and override configure method

get(a_type: Callable[[...], T]) → T[source]

Get an instance of type T

Parameters:a_type – either a class T or a function returning a T
Returns:an instance of T
class smart_injector.Lifetime[source]

Specifies the lifetime for objects created by the container

Lifetime.SINGLETON:
 smart_injector.StaticContainer.get() returns the same every instance on every call
Lifetime.TRANSIENT:
 smart_injector.StaticContainer.get() returns a new instance on every call
class smart_injector.Config(backend: smart_injector.config.backend.ConfigBackend)[source]

Used by the user to configure DI container injection behaviour

arg_factory(a_type: Callable[[...], T], where: Optional[Type[T]] = None, **kwargs)[source]

In difference to arguments(): Instead of providing a value for parameter directly, factory is called to get the value for the parameter.

Parameters:
  • a_type
  • where
  • kwargs
Returns:

arguments(a_type: Callable[[...], T], where: Optional[Type[T]] = None, **kwargs)[source]

When creating an object of type T, the provided arguments will be inserted in T’s constructor (if it is a class) or a_type will be called with this arguments if it is a function.

Parameters:
  • a_type
  • where
  • kwargs
Returns:

Note

Only keyword arguments are supported

bind(a_type: Callable[[...], T], to_type: Callable[[...], S], where: Optional[Type[T]] = None)[source]

Specify a binding. Whenever an object of type a_type is required, then an object of type to_type will be provided. For example you can configure, which concrete class shall be used for an abstract base class

Parameters:
  • a_type – will be replaced by to_type
  • to_type – will be used when an object of type a_type is required. to_type must be a subclass of a_type
  • where
  • kwargs
Returns:

dependency(a_type: Callable[[...], T])[source]

declare that T is a dependency for the container. When creating the container with smart_injector.create_container() an instance must be provided for every dependency which was declared.

Parameters:a_type
Returns:
instance(a_type: Callable[[...], T], instance: T, where: Optional[Type[T]] = None)[source]

set an instance of type T which is returned whenever an object of type T is requested

Parameters:
  • a_type
  • instance
  • where
Returns:

lifetime(a_type: Callable[[...], T], lifetime: smart_injector.lifetime.Lifetime, where: Optional[Type[T]] = None)[source]

Specify the lifetime for an object of type T. See smart_injector.Lifetime()

Parameters:
  • a_type
  • lifetime
  • where
Returns:

None