Reference

Injection decorators

tinyioc.inject(module: ~typing.Type[~tinyioc.decorators.E] = <class 'tinyioc.module.module.GlobalModule'>)

Inject one or more services from the IOC container into the function. If the function named arguments already include the service name, the argument is NOT overwritten, instead forwarding the original one.

Example:

@inject()
def endpoint(database_service: DbService):
    user = database_service.getUser(...)
    ...

This behavior is optimal for tests, where you call the function with your mocked object. e.g.

@inject()
def my_fun(api_service: ApiService):
    ...

# -----------------------
# Test:
api_mock = ApiServiceMock()
my_fun(api_service=api_mock)
...
Parameters:

module – The module to retrieve the service from (defaults to the global module)

tinyioc.injectable(scope: ~tinyioc.types.ServiceLifetime = ServiceLifetime.SINGLETON, module: ~typing.Type[~tinyioc.decorators.E] = <class 'tinyioc.module.module.GlobalModule'>, register_for: ~typing.Type[~tinyioc.decorators.K] | None = None, **kwargs)

Registers the class into the IOC container

Example:

@injectable(ServiceLifetime.SINGLETON)
class MailService:
    ...
Parameters:
  • scope – The scope of this service (singleton, transient)

  • module – The module to register this service into

  • register_for – Register this instance for the provided class-interface

  • kwargs – Params to call the class constructor with

Injection helper methods

tinyioc.get_service(cls: ~typing.Type[~tinyioc.helpers.T], module: ~typing.Type[~tinyioc.helpers.E] = <class 'tinyioc.module.module.GlobalModule'>) T | None

Retrieve a service from the container

Parameters:
  • cls – The service class

  • module – The module to retrieve the service from

Returns:

The service, or None if it couldn’t be retrieved

tinyioc.register_instance(instance: ~tinyioc.helpers.T, module: ~typing.Type[~tinyioc.helpers.E] = <class 'tinyioc.module.module.GlobalModule'>, register_for: ~typing.Type[~tinyioc.helpers.K] | None = None)

Register the instance of a service

Parameters:
  • instance – The instance of the service

  • module – The module to register this instance into

  • register_for – The class-interface to register this instance as

tinyioc.register_singleton(cls: ~typing.Type[~tinyioc.helpers.T], module: ~typing.Type[~tinyioc.helpers.E] = <class 'tinyioc.module.module.GlobalModule'>, register_for: ~typing.Type[~tinyioc.helpers.K] | None = None, **kwargs)

Register a class with singleton scope (one instance shared across the module)

Parameters:
  • cls – The class to register

  • module – The module to register the service into

  • register_for – The class-interface to register this service as

tinyioc.register_transient(cls: ~typing.Type[~tinyioc.helpers.T], module: ~typing.Type[~tinyioc.helpers.E] = <class 'tinyioc.module.module.GlobalModule'>, register_for: ~typing.Type[~tinyioc.helpers.K] | None = None, **kwargs)

Register a class with transient scope (new instance every time the service is injected)

Parameters:
  • cls – The class to register

  • module – The module to register the service into

  • register_for – The class-interface to register this service as

tinyioc.unregister_service(cls: ~typing.Type[~tinyioc.helpers.T], module: ~typing.Type[~tinyioc.helpers.E] = <class 'tinyioc.module.module.GlobalModule'>)

Unregister a service

Parameters:
  • cls – The service class

  • module – The module from which to unregister

Modules

class tinyioc.FromModule(module: Type[E])

Bases: object

Helper class to define the module from which a dependency is injected

Example:

@inject()
def my_fun(dep_a: DependencyA = FromModule(ModuleA), dep_b: DependencyB = FromModule(ModuleB)):
    ...
class tinyioc.IocModule

Bases: object

Base class for a module. All modules must inherit from this class

provides: List[Provide]
services: Dict[Type[T], ServiceEntry[T]]
tinyioc.module()

Declares a module to scope the injected services

Example:

@module()
class MyModule(IocModule):
    pass

Module providers

class tinyioc.ProvideInstance(entry: T, provide_for: Type[E] | None = None)

Bases: Provide

Class for providing instances in modules

entry: T | Type[T]
kwargs: Dict | None
provide_for: Type[E] | None
class tinyioc.ProvideSingleton(entry: Type[T], provide_for: Type[E] | None = None, **kwargs)

Bases: Provide

Class for providing singletons in modules

entry: T | Type[T]
kwargs: Dict | None
provide_for: Type[E] | None
class tinyioc.ProvideTransient(entry: Type[T], provide_for: Type[E] | None = None, **kwargs)

Bases: Provide

Class for providing transient services in modules

entry: T | Type[T]
kwargs: Dict | None
provide_for: Type[E] | None

Service lifetime

class tinyioc.ServiceLifetime(value)

Bases: Enum

The service lifetime. Can be singleton (one instance shared through the whole app) or transient (new instance every time it is injected)

SINGLETON = 0

Singleton scope: one instance shared through the whole app

TRANSIENT = 1

Transient scope: new instance every time it is injected