Skip to content

Commit

Permalink
fix overriding in child container
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnik512 committed Nov 13, 2024
1 parent 7e142f2 commit 5b8a4e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/modern-di/modern_di/providers/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
21 changes: 11 additions & 10 deletions packages/modern-di/tests_core/providers/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5b8a4e7

Please sign in to comment.