From 5b8a4e7ecd41e7dbf148a2127841a707bd8759b8 Mon Sep 17 00:00:00 2001 From: Artur Shiriev Date: Wed, 13 Nov 2024 11:55:04 +0300 Subject: [PATCH] fix overriding in child container --- .../modern-di/modern_di/providers/abstract.py | 4 ++-- .../tests_core/providers/test_factory.py | 21 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/modern-di/modern_di/providers/abstract.py b/packages/modern-di/modern_di/providers/abstract.py index cbd4571..05fe510 100644 --- a/packages/modern-di/modern_di/providers/abstract.py +++ b/packages/modern-di/modern_di/providers/abstract.py @@ -39,10 +39,10 @@ def _check_providers_scope(self, providers: typing.Iterable[typing.Any]) -> None class AbstractOverrideProvider(AbstractProvider[T_co], abc.ABC): def override(self, override_object: object, container: Container) -> None: - container.override(self.provider_id, override_object) + container.find_container(self.scope).override(self.provider_id, override_object) def reset_override(self, container: Container) -> None: - container.reset_override(self.provider_id) + container.find_container(self.scope).reset_override(self.provider_id) class AbstractCreatorProvider(AbstractOverrideProvider[T_co], abc.ABC): diff --git a/packages/modern-di/tests_core/providers/test_factory.py b/packages/modern-di/tests_core/providers/test_factory.py index 20ac7f4..8e66f3a 100644 --- a/packages/modern-di/tests_core/providers/test_factory.py +++ b/packages/modern-di/tests_core/providers/test_factory.py @@ -59,21 +59,22 @@ async def test_app_factory_in_request_scope() -> None: async def test_factory_overridden() -> None: async with Container(scope=Scope.APP) as app_container: - instance1 = app_factory.sync_resolve(app_container) + with app_container.build_child_container(scope=Scope.REQUEST) as request_container: + instance1 = app_factory.sync_resolve(app_container) - app_factory.override(SimpleCreator(dep1="override"), container=app_container) + app_factory.override(SimpleCreator(dep1="override"), container=request_container) - instance2 = app_factory.sync_resolve(app_container) - instance3 = await app_factory.async_resolve(app_container) - assert instance1 is not instance2 - assert instance2 is instance3 - assert instance2.dep1 != instance1.dep1 + instance2 = app_factory.sync_resolve(app_container) + instance3 = await app_factory.async_resolve(app_container) + assert instance1 is not instance2 + assert instance2 is instance3 + assert instance2.dep1 != instance1.dep1 - app_factory.reset_override(app_container) + app_factory.reset_override(app_container) - instance4 = app_factory.sync_resolve(app_container) + instance4 = app_factory.sync_resolve(app_container) - assert instance4.dep1 == instance1.dep1 + assert instance4.dep1 == instance1.dep1 assert instance3 is not instance4