Перейти к содержанию

Inject, Factory and Injects

Warning

The current page still doesn't have a translation for this language.

But you can help translating it: Contributing.

Esmerald dependency injection system is actually pretty simple and can be checked in the official dependency injection section for more details.

from esmerald import Inject, Injects, Factory, DiderectInjects

esmerald.Inject

Inject(dependency, use_cache=False, **kwargs)

Bases: ArbitraryHashableBaseModel

PARAMETER DESCRIPTION
dependency

TYPE: AnyCallable

use_cache

TYPE: bool DEFAULT: False

**kwargs

TYPE: Any DEFAULT: {}

Source code in esmerald/injector.py
56
57
58
59
60
61
def __init__(self, dependency: "AnyCallable", use_cache: bool = False, **kwargs: Any):
    super().__init__(**kwargs)
    self.dependency = dependency
    self.signature_model: Optional["Type[SignatureModel]"] = None
    self.use_cache = use_cache
    self.value: Any = Void

dependency instance-attribute

dependency = dependency

signature_model instance-attribute

signature_model = None

use_cache instance-attribute

use_cache = use_cache

value instance-attribute

value = Void

esmerald.Injects

Injects(default=Undefined, skip_validation=False, allow_none=True)

Bases: FieldInfo

Creates a FieldInfo class with extra parameters. This is used for dependencies and to inject them.

Example

@get(dependencies={"value": Inject(lambda: 13)})
def myview(value: Injects()):
    return {"value": value}
PARAMETER DESCRIPTION
default

TYPE: Any DEFAULT: Undefined

skip_validation

TYPE: bool DEFAULT: False

allow_none

TYPE: bool DEFAULT: True

Source code in esmerald/params.py
622
623
624
625
626
627
628
629
630
631
632
633
634
def __init__(
    self,
    default: Any = Undefined,
    skip_validation: bool = False,
    allow_none: bool = True,
) -> None:
    self.allow_none = allow_none
    self.extra: Dict[str, Any] = {
        IS_DEPENDENCY: True,
        SKIP_VALIDATION: skip_validation,
        "allow_none": self.allow_none,
    }
    super().__init__(default=default, json_schema_extra=self.extra)

allow_none instance-attribute

allow_none = allow_none

extra instance-attribute

extra = {IS_DEPENDENCY: True, SKIP_VALIDATION: skip_validation, 'allow_none': allow_none}

esmerald.Factory

Factory(provides, *args)

The provider can be passed in separate ways. Via direct callable or via string value where it will be automatically imported by the application.

PARAMETER DESCRIPTION
provides

TYPE: Union[AnyCallable, str]

*args

TYPE: Any DEFAULT: ()

Source code in esmerald/injector.py
14
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(self, provides: Union["AnyCallable", str], *args: Any) -> None:
    """
    The provider can be passed in separate ways. Via direct callable
    or via string value where it will be automatically imported by the application.
    """
    self.__args: Tuple[Any, ...] = ()
    self.set_args(*args)
    self.is_nested: bool = False

    if isinstance(provides, str):
        self.provides, self.is_nested = load_provider(provides)
    else:
        self.provides = provides

__args instance-attribute

__args = ()

is_nested instance-attribute

is_nested = False

provides instance-attribute

provides = provides

cls property

cls

Return provided type.

set_args

set_args(*args)
PARAMETER DESCRIPTION
*args

TYPE: Any DEFAULT: ()

Source code in esmerald/injector.py
28
29
def set_args(self, *args: Any) -> None:
    self.__args = args