This commit is contained in:
Pablo Valverde 2025-12-10 14:52:44 +00:00 committed by GitHub
commit 38c0887b7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 55 additions and 4 deletions

View File

@ -959,8 +959,8 @@ class ProvidedInstance(Modifier):
self.segments.append((self.TYPE_ITEM, item))
return self
def call(self) -> Self:
self.segments.append((self.TYPE_CALL, None))
def call(self, *args, **kwargs) -> Self:
self.segments.append((self.TYPE_CALL, (args, kwargs)))
return self
def modify(
@ -975,7 +975,7 @@ class ProvidedInstance(Modifier):
elif type_ == ProvidedInstance.TYPE_ITEM:
provider = provider[value]
elif type_ == ProvidedInstance.TYPE_CALL:
provider = provider.call()
provider = provider.call(*value[0], **value[1])
else:
assert_never(type_)
return provider

View File

@ -1,6 +1,6 @@
from dependency_injector import containers, providers
from .service import Service
from .service import Service, ServiceWithCallable
class SubContainer(containers.DeclarativeContainer):
@ -14,4 +14,6 @@ class Container(containers.DeclarativeContainer):
service = providers.Factory(Service)
service_with_callable = providers.Factory(ServiceWithCallable)
sub = providers.Container(SubContainer)

View File

@ -100,6 +100,27 @@ def test_provided_instance(some_value: int = Provide[Container.service.provided.
return some_value
@inject
def test_provided_instance_call_with_args(
some_value: int = Provide[Container.service_with_callable.provided.method_with_args.call(1, 2)]
):
return some_value
@inject
def test_provided_instance_call_with_kwargs(
some_value: dict = Provide[Container.service_with_callable.provided.method_with_kwargs.call(a=1, b=2)]
):
return some_value
@inject
def test_provided_instance_call_with_args_and_kwargs(
some_value: dict = Provide[Container.service_with_callable.provided.foo.process.call(1, 2, key="value")]
):
return some_value
@inject
def test_subcontainer_provider(some_value: int = Provide[Container.sub.int_object]):
return some_value

View File

@ -1,2 +1,15 @@
class Service:
service_attr: int
class ServiceWithCallable:
def __init__(self):
self.foo = CallableDict({"bar": lambda: 10})
self.method_with_args = lambda x, y: x + y
self.method_with_kwargs = lambda **kwargs: kwargs
class CallableDict(dict):
def __init__(self, data):
super().__init__(data)
self.process = lambda *args, **kwargs: {"args": args, "kwargs": kwargs}

View File

@ -188,6 +188,21 @@ def test_provided_instance(container: Container):
assert some_value == 10
def test_provided_instance_call_with_args():
some_value = module.test_provided_instance_call_with_args()
assert some_value == 3
def test_provided_instance_call_with_kwargs():
some_value = module.test_provided_instance_call_with_kwargs()
assert some_value == {"a": 1, "b": 2}
def test_provided_instance_call_with_args_and_kwargs():
some_value = module.test_provided_instance_call_with_args_and_kwargs()
assert some_value == {"args": (1, 2), "kwargs": {"key": "value"}}
def test_subcontainer():
some_value = module.test_subcontainer_provider()
assert some_value == 1