Skip to content

Commit

Permalink
Merge 781b3a6 into 3e207a4
Browse files Browse the repository at this point in the history
  • Loading branch information
rmk135 committed Jan 15, 2021
2 parents 3e207a4 + 781b3a6 commit bf46c44
Show file tree
Hide file tree
Showing 8 changed files with 2,093 additions and 1,389 deletions.
7 changes: 7 additions & 0 deletions docs/main/changelog.rst
Expand Up @@ -7,6 +7,13 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_

Development version
-------------------
- Add ``.dependencies`` attribute to the ``DeclarativeContainer`` and ``DynamicContainer``.
It returns dictionary of container ``Dependency`` and ``DependenciesContainer`` providers.
See issue `#357 <https://github.com/ets-labs/python-dependency-injector/issues/357>`_.
Many thanks to `Shaun Cutts <https://github.com/shaunc>`_ for suggesting the feature.

4.8.3
-----
- Fix a bug in the ``Configuration`` provider to correctly handle overriding by ``None``.
Expand Down
3,392 changes: 2,003 additions & 1,389 deletions src/dependency_injector/containers.c

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/dependency_injector/containers.pyi
Expand Up @@ -24,6 +24,7 @@ C_Overriding = TypeVar('C_Overriding', bound='DeclarativeContainer')
class Container:
provider_type: Type[Provider] = Provider
providers: Dict[str, Provider]
dependencies: Dict[str, Provider]
overridden: Tuple[Provider]
__self__: Provider
def __init__(self) -> None: ...
Expand Down
34 changes: 34 additions & 0 deletions src/dependency_injector/containers.pyx
Expand Up @@ -15,6 +15,8 @@ from .providers cimport (
Provider,
Object,
Resource,
Dependency,
DependenciesContainer,
Container as ContainerProvider,
deepcopy,
)
Expand Down Expand Up @@ -125,6 +127,22 @@ class DynamicContainer(object):
del self.providers[name]
super(DynamicContainer, self).__delattr__(name)

@property
def dependencies(self):
"""Return dependency providers dictionary.
Dependency providers can be both of :py:class:`dependency_injector.providers.Dependency` and
:py:class:`dependency_injector.providers.DependenciesContainer`.
:rtype:
dict[str, :py:class:`dependency_injector.providers.Provider`]
"""
return {
name: provider
for name, provider in self.providers.items()
if isinstance(provider, (Dependency, DependenciesContainer))
}

def set_providers(self, **providers):
"""Set container providers.
Expand Down Expand Up @@ -340,6 +358,22 @@ class DeclarativeContainerMetaClass(type):
del cls.cls_providers[name]
super(DeclarativeContainerMetaClass, cls).__delattr__(name)

@property
def dependencies(cls):
"""Return dependency providers dictionary.
Dependency providers can be both of :py:class:`dependency_injector.providers.Dependency` and
:py:class:`dependency_injector.providers.DependenciesContainer`.
:rtype:
dict[str, :py:class:`dependency_injector.providers.Provider`]
"""
return {
name: provider
for name, provider in cls.providers.items()
if isinstance(provider, (Dependency, DependenciesContainer))
}


@six.add_metaclass(DeclarativeContainerMetaClass)
class DeclarativeContainer(object):
Expand Down
10 changes: 10 additions & 0 deletions tests/typing/declarative_container.py
@@ -1,3 +1,5 @@
from typing import Dict

from dependency_injector import containers, providers


Expand Down Expand Up @@ -38,3 +40,11 @@ class Container4(containers.DeclarativeContainer):

container4 = Container4()
container4.override(Container4())


# Test 5: to check .dependencies attribute
class Container5(containers.DeclarativeContainer):
provider = providers.Factory(int)


dependencies: Dict[str, providers.Provider] = Container5.dependencies
6 changes: 6 additions & 0 deletions tests/typing/dynamic_container.py
@@ -1,3 +1,5 @@
from typing import Dict

from dependency_injector import containers, providers


Expand All @@ -16,3 +18,7 @@
# Test 4: to check set_providers()
container4 = containers.DynamicContainer()
container4.set_providers(a=providers.Provider())

# Test 5: to check .dependencies attribute
container5 = containers.DynamicContainer()
dependencies: Dict[str, providers.Provider] = container5.dependencies
26 changes: 26 additions & 0 deletions tests/unit/containers/test_declarative_py2_py3.py
Expand Up @@ -60,6 +60,32 @@ def test_inherited_providers_attribute(self):
p21=ContainerB.p21,
p22=ContainerB.p22))

def test_dependencies_attribute(self):
class ContainerD(ContainerC):
p41 = providers.Dependency()
p42 = providers.DependenciesContainer()

class ContainerE(ContainerD):
p51 = providers.Dependency()
p52 = providers.DependenciesContainer()

self.assertEqual(
ContainerD.dependencies,
{
'p41': ContainerD.p41,
'p42': ContainerD.p42,
},
)
self.assertEqual(
ContainerE.dependencies,
{
'p41': ContainerD.p41,
'p42': ContainerD.p42,
'p51': ContainerE.p51,
'p52': ContainerE.p52,
},
)

def test_set_get_del_providers(self):
a_p13 = providers.Provider()
b_p23 = providers.Provider()
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/containers/test_dynamic_py2_py3.py
Expand Up @@ -24,6 +24,12 @@ def test_providers_attribute(self):
self.assertIsNot(container_a1.p12, container_a2.p12)
self.assertNotEqual(container_a1.providers, container_a2.providers)

def test_dependencies_attribute(self):
container = ContainerA()
container.a1 = providers.Dependency()
container.a2 = providers.DependenciesContainer()
self.assertEqual(container.dependencies, {'a1': container.a1, 'a2': container.a2})

def test_set_get_del_providers(self):
p13 = providers.Provider()

Expand Down

0 comments on commit bf46c44

Please sign in to comment.