mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Remove restriction to wire a dynamic container
This commit is contained in:
parent
ee89476db0
commit
6c1b7cc677
|
@ -322,9 +322,6 @@ def wire( # noqa: C901
|
|||
packages: Optional[Iterable[ModuleType]] = None,
|
||||
) -> None:
|
||||
"""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:
|
||||
modules = []
|
||||
|
||||
|
@ -655,12 +652,6 @@ def _is_patched(fn):
|
|||
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:
|
||||
return (isinstance(instance, type)
|
||||
and getattr(instance, '__IS_CONTAINER__', False) is True
|
||||
|
|
|
@ -7,7 +7,7 @@ from dependency_injector.wiring import (
|
|||
Provider,
|
||||
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
|
||||
import os
|
||||
|
@ -335,6 +335,27 @@ class WiringAndFastAPITest(unittest.TestCase):
|
|||
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):
|
||||
|
||||
def test_async_injections(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user