class X(containers.DeclarativeContainer): foo = providers.Dependency(instance_of=str)
def setUp(self): self.provider = providers.Dependency(instance_of=list)
def test_set_default_returns_self(self): provider = providers.Dependency() self.assertIs(provider.set_default(providers.Provider()), provider)
class Services(containers.DeclarativeContainer): a = providers.Dependency() c = providers.Factory(C, a=a) b = providers.Factory(B, fa=a.delegate())
class _Container(containers.DeclarativeContainer): p1 = providers.Dependency(instance_of=int) p2 = providers.Dependency(object) p2.override(providers.Factory(dict, p1=p1))
def test_is_defined_with_default(): provider = providers.Dependency(default="value") assert provider.is_defined is True
class ContainerD(ContainerC): p41 = providers.Dependency() p42 = providers.DependenciesContainer()
class Container(containers.DeclarativeContainer): cache = providers.Dependency(instance_of=Cache, default=InMemoryCache())
def test_parent_name_is_none(): provider = providers.Dependency() assert provider.parent_name is None
def test_default_attribute_provider(self): default = providers.Factory(dict, foo='bar') provider = providers.Dependency(instance_of=dict, default=default) self.assertEqual(provider.default(), {'foo': 'bar'}) self.assertIs(provider.default, default)
raise NotImplementedError class CategoryFactory: """Factory to create new Category entities. Note that the caller is responsible to save the created instances using the CategoryRepository.""" def __init__(self, category_repository): self.repository = category_repository def create_category(self, name, parent=None): category = Category(uuid.uuid4().hex, name, parent and self.repository.get_category_by_qualified_name(parent.qualified_name) or None) return category def create_category_from_qualified_name(self, qualified_name): category = None if not category: next_parent = None for name in qualified_name.split("::"): tmp_category = self.create_category(name, next_parent) category = self.repository.get_category_by_qualified_name(tmp_category.qualified_name) or tmp_category category.parent = next_parent next_parent = category return category logger.info("creating CategoryRepository dependency") category_repository = providers.Dependency(instance_of=CategoryRepository) category_factory = providers.Singleton(CategoryFactory, category_repository)
def test_default_provider(self): provider = providers.Dependency(instance_of=dict, default=providers.Factory(dict, foo='bar')) self.assertEqual(provider.default(), {'foo': 'bar'})
class ChildContainer(containers.DeclarativeContainer): dependency = providers.Dependency()
class Container(containers.DeclarativeContainer): dependency = providers.Dependency() dependencies_container = providers.DependenciesContainer() container = providers.Container(ContainerA)
def provider(): return providers.Dependency(instance_of=list)
def test_forward_attr_to_default(): default = providers.Configuration() provider = providers.Dependency(default=default) provider.from_dict({"foo": "bar"}) assert default() == {"foo": "bar"}
def test_default_attribute_provider(): default = providers.Factory(dict, foo="bar") provider = providers.Dependency(instance_of=dict, default=default) assert provider.default() == {"foo": "bar"} assert provider.default is default
class Container(containers.DeclarativeContainer): provider = providers.Dependency(dict, default=default)
def test_parent_set_on__init__(): provider = providers.Dependency() container = providers.DependenciesContainer(name=provider) assert container.name is provider assert container.name.parent is container
def test_init_with_not_class(): with raises(TypeError): providers.Dependency(object())
class ContainerE(ContainerD): p51 = providers.Dependency() p52 = providers.DependenciesContainer()
class Container(containers.DeclarativeContainer): provider = providers.Dependency(dict, default=providers.Factory( dict, foo=providers.Object(bar)))
from typing import Type from dependency_injector import providers class Animal: ... class Cat(Animal): def __init__(self, *_, **__): ... # Test 1: to check the return type provider1 = providers.Dependency(instance_of=Animal) provider1.override(providers.Factory(Cat)) var1: Animal = provider1() # Test 2: to check the return type provider2 = providers.Dependency(instance_of=Animal) var2: Type[Animal] = provider2.instance_of # Test 3: to check the return type with await provider3 = providers.Dependency(instance_of=Animal) async def _async3() -> None: var1: Animal = await provider3() # type: ignore var2: Animal = await provider3.async_()
class Container(containers.DeclarativeContainer): dependency = providers.Dependency(instance_of=int)
class _Container(containers.DeclarativeContainer): p1 = providers.Dependency(instance_of=int) p2 = providers.Factory(p1)
def test_default_attribute(): provider = providers.Dependency(instance_of=dict, default={"foo": "bar"}) assert provider.default() == {"foo": "bar"}
def test_set_instance_of_returns_self(self): provider = providers.Dependency() self.assertIs(provider.set_instance_of(list), provider)
def test_default_provider(): provider = providers.Dependency(instance_of=dict, default=providers.Factory(dict, foo="bar")) assert provider.default() == {"foo": "bar"}
def test_default_attribute(self): provider = providers.Dependency(instance_of=dict, default={'foo': 'bar'}) self.assertEqual(provider.default(), {'foo': 'bar'})
class MailContainer(containers.DeclarativeContainer): notifier_service: Dependency[NotifierService] = providers.Dependency()