python-dependency-injector/examples/containers/declarative_inheritance.py

35 lines
753 B
Python
Raw Permalink Normal View History

2020-08-14 23:27:17 +03:00
"""Declarative containers inheritance example."""
2016-05-29 22:06:02 +03:00
2020-08-14 23:27:17 +03:00
from dependency_injector import containers, providers
2016-05-29 22:06:02 +03:00
class ContainerA(containers.DeclarativeContainer):
provider1 = providers.Factory(object)
class ContainerB(ContainerA):
provider2 = providers.Singleton(object)
2020-08-14 23:27:17 +03:00
assert ContainerA.providers == {
2021-09-30 22:16:17 +03:00
"provider1": ContainerA.provider1,
2020-08-14 23:27:17 +03:00
}
assert ContainerB.providers == {
2021-09-30 22:16:17 +03:00
"provider1": ContainerA.provider1,
"provider2": ContainerB.provider2,
2020-08-14 23:27:17 +03:00
}
assert ContainerA.cls_providers == {
2021-09-30 22:16:17 +03:00
"provider1": ContainerA.provider1,
2020-08-14 23:27:17 +03:00
}
assert ContainerB.cls_providers == {
2021-09-30 22:16:17 +03:00
"provider2": ContainerB.provider2,
2020-08-14 23:27:17 +03:00
}
assert ContainerA.inherited_providers == {}
assert ContainerB.inherited_providers == {
2021-09-30 22:16:17 +03:00
"provider1": ContainerA.provider1,
2020-08-14 23:27:17 +03:00
}