Class Registration

The class registry stores which objects can be injected and how to

maintain cache control over them.

class autoinject.class_registry.CacheStrategy(*values)[source]

Defines how caching should be managed for this object

CONTEXT_CACHE = 3[source]

A single instance of the object is allowed per context. What a context is can vary by application; for example, in the context of a WSGI application, each request might be an individual context. Specify this when each thread or context might need its own copy of the object.

GLOBAL_CACHE = 2[source]

A single instance of the object is allowed. Specify this for thread-safe objects that can manage a single global instance even in a multi-threaded environment.

NO_CACHE = 1[source]

No caching allowed. Specify this when instances of the object should not be shared.

class autoinject.class_registry.ClassInfo(cache_key: str, strategy: autoinject.class_registry.CacheStrategy, constructor: autoinject.class_registry.InjectableConstructor, ignore_informants: List[str] | None = None)[source]
exception autoinject.class_registry.ClassNotFoundException(cls_name: str)[source]

Raised when a class is requested that has not been registered.

Parameters:

cls_name (str) – Name of the class not found in the registry

class autoinject.class_registry.ClassRegistry[source]

Manages a list of classes and how they can be instantiated.

static cls_to_str(cls: type | str) str[source]

Converts a type to a string that represents the fully-qualified name of the class. :param cls: Either a type to convert or a string representing the fully-qualified name of the class. :type cls: type OR str :return: Returns a string that could be used to import the class :rtype: str

static cls_to_type(cls: type | str) type[source]

Converts a type to a string that represents the fully-qualified name of the class. :param cls: Either a type to convert or a string representing the fully-qualified name of the class. :type cls: type OR str :return: Returns a string that could be used to import the class :rtype: str

get_class_info(cls: type | str) ClassInfo[source]

Retrieves an instance of cls.

This method searches the registered classes for the spec on how to build an object of type cls and calls the specified constructor method (usually the class itself).

Note that caching is not implemented here, caching is provided by autoinject.cache_manager.CacheManager instead which wraps around this class.

Parameters:

cls (type OR str) – The class to get an instance of

Returns:

An instance of cls

Return type:

cls

get_injectable_attributes(cls: type) List[AttributeReplacement][source]

Given a type, find all members we should check for injection

get_injectable_parameters(func: Callable | type | classmethod | staticmethod) List[ParameterReplacement][source]

Inspects the given callable object and gets a list of parameters that need to be managed.

Parameters:
  • func – The callable to inspect

  • cls_registry – The class registry to check for injectable or delayed parameters

Returns:

A list of ParameterReplacement objects

Return type:

t.List[ParameterReplacement]

is_injectable(cls: type | str) bool[source]

Checks if the given class is injectable

Parameters:

cls (type OR str) – The class that is being checked

Returns:

Whether the class provided can be injected

Return type:

bool

register(cls: type | str, *args, constructor: Callable[[...], Any] | type | str | None = None, weight: int | None = None, caching_strategy: CacheStrategy | None = None, as_weakref: bool | None = None, ignore_informants: List[str] | None = None, **kwargs)[source]

Registers a class for injection and specifies how to construct it

The default method of construction is to call cls itself with args and kwargs, i.e.:

cls(*args, **kwargs)

Should more control over the construction of an object be required, constructor can be specified as any callable object. Construction is then done as follows:

constructor(*args, **kwargs)

Parameters:
  • ignore_informants – A list of informant names to ignore, None to respect them all. Only works when cache_strategy is None or autoinject.class_registry.CacheStrategy.CONTEXT_CACHE.

  • as_weakref – Set to true to force the cache manager to keep only a weakref to the object

  • cls (type or str) – The type to inject or a unique identifier

  • args (any) – Positional arguments to pass to the constructor

  • constructor (callable or None) – Optional callable to construct an object when required. Defaults to calling cls directly

  • caching_strategy (autoinject.class_registry.CacheStrategy or None) – Specify how instances of this class are to be cached. Defaults to autoinject.class_registry.CacheStrategy.CONTEXT_CACHE, i.e. different objects by context

  • weight (int) – Higher values override lower values

  • kwargs (any) – Keyword arguments to pass to the constructor

class autoinject.class_registry.InjectableConstructor(constructor: Callable[..., T], args: Sequence, kwargs: dict, weight: int, strategy: autoinject.class_registry.CacheStrategy, ignore_informants: List[str] | None = None, as_weakref: bool = False)[source]
class autoinject.class_registry.InjectableInfo(constructors: List[autoinject.class_registry.InjectableConstructor])[source]