Remove restriction to wire a dynamic container

This commit is contained in:
Roman Mogylatov 2021-03-19 20:23:10 -04:00
parent ee89476db0
commit 6c1b7cc677
2 changed files with 22 additions and 10 deletions

View File

@ -322,9 +322,6 @@ def wire( # noqa: C901
packages: Optional[Iterable[ModuleType]] = None, packages: Optional[Iterable[ModuleType]] = None,
) -> None: ) -> None:
"""Wire container providers with provided packages and modules.""" """Wire container providers with provided packages and modules."""
if not _is_declarative_container_instance(container):
raise Exception('Can wire only an instance of the declarative container')
if not modules: if not modules:
modules = [] modules = []
@ -655,12 +652,6 @@ def _is_patched(fn):
return getattr(fn, '__wired__', False) is True return getattr(fn, '__wired__', False) is True
def _is_declarative_container_instance(instance: Any) -> bool:
return (not isinstance(instance, type)
and getattr(instance, '__IS_CONTAINER__', False) is True
and getattr(instance, 'declarative_parent', None) is not None)
def _is_declarative_container(instance: Any) -> bool: def _is_declarative_container(instance: Any) -> bool:
return (isinstance(instance, type) return (isinstance(instance, type)
and getattr(instance, '__IS_CONTAINER__', False) is True and getattr(instance, '__IS_CONTAINER__', False) is True

View File

@ -7,7 +7,7 @@ from dependency_injector.wiring import (
Provider, Provider,
Closing, Closing,
) )
from dependency_injector import errors from dependency_injector import containers, providers, errors
# Runtime import to avoid syntax errors in samples on Python < 3.5 # Runtime import to avoid syntax errors in samples on Python < 3.5
import os import os
@ -335,6 +335,27 @@ class WiringAndFastAPITest(unittest.TestCase):
self.assertIsNot(result_1, result_2) self.assertIsNot(result_1, result_2)
class WireDynamicContainerTest(unittest.TestCase):
def test_wire(self):
sub = containers.DynamicContainer()
sub.int_object = providers.Object(1)
container = containers.DynamicContainer()
container.config = providers.Configuration()
container.service = providers.Factory(Service)
container.sub = sub
container.wire(
modules=[module],
packages=[package],
)
self.addCleanup(container.unwire)
service = module.test_function()
self.assertIsInstance(service, Service)
class WiringAsyncInjectionsTest(AsyncTestCase): class WiringAsyncInjectionsTest(AsyncTestCase):
def test_async_injections(self): def test_async_injections(self):