Specialization of containersΒΆ

DeclarativeContainer could be specialized for any kind of needs via declaring its subclasses.

One of such builtin features is a limitation for providers type.

Next example shows usage of this feature with DeclarativeContainer in couple with feature of dependency_injector.providers.Factory for limitation of its provided type:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Specializing declarative container and factory provider example."""

import collections

import dependency_injector.containers as containers
import dependency_injector.providers as providers
import dependency_injector.errors as errors


class SequenceProvider(providers.Factory):
    """Sequence factory.

    Can provide only sequence objects.
    """

    provided_type = collections.Sequence


class SequencesContainer(containers.DeclarativeContainer):
    """IoC container.

    Can contain only sequence providers.
    """

    provider_type = SequenceProvider


if __name__ == '__main__':
    try:
        class _SequenceContainer1(SequencesContainer):
            object_provider = providers.Factory(object)
    except errors.Error as exception:
        print(exception)
        # <class '__main__._SequenceContainer1'> can contain only
        # <class '__main__.SequenceProvider'> instances

    try:
        class _SequenceContainer2(SequencesContainer):
            object_provider = SequenceProvider(object)
    except errors.Error as exception:
        print(exception)
        # <class '__main__.SequenceProvider'> can provide only
        # <class '_abcoll.Sequence'> instances

    class _SequenceContaier3(SequencesContainer):
        list_provider = SequenceProvider(list)

    assert _SequenceContaier3.list_provider() == list()

Limitation for providers type could be used with DynamicContainer as well:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Specializing dynamic container and factory provider example."""

import collections

import dependency_injector.containers as containers
import dependency_injector.providers as providers
import dependency_injector.errors as errors


class SequenceProvider(providers.Factory):
    """Sequence factory.

    Can provide only sequence objects.
    """

    provided_type = collections.Sequence


sequences_container = containers.DynamicContainer()
sequences_container.provider_type = SequenceProvider


if __name__ == '__main__':
    try:
        sequences_container.object_provider = providers.Factory(object)
    except errors.Error as exception:
        print(exception)
        # <dependency_injector.containers.DynamicContainer object at
        # 0x107820ed0> can contain only <class '__main__.SequenceProvider'>
        # instances

    try:
        sequences_container.object_provider = SequenceProvider(object)
    except errors.Error as exception:
        print(exception)
        # <class '__main__.SequenceProvider'> can provide only
        # <class '_abcoll.Sequence'> instances

    sequences_container.list_provider = SequenceProvider(list)

    assert sequences_container.list_provider() == list()