Skip to content

Commit 7dfb09f

Browse files
authored
Merge pull request #118 from modern-python/fix-resolving
fix resolving in incorrect scope
2 parents b1efa9c + 1524c25 commit 7dfb09f

6 files changed

Lines changed: 9 additions & 4 deletions

File tree

packages/modern-di-fastapi/modern_di_fastapi/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
fastapi_request = providers.ContextProvider(scope=Scope.REQUEST, context_type=fastapi.Request)
15-
fastapi_websocket = providers.ContextProvider(scope=Scope.REQUEST, context_type=fastapi.WebSocket)
15+
fastapi_websocket = providers.ContextProvider(scope=Scope.SESSION, context_type=fastapi.WebSocket)
1616

1717

1818
def fetch_di_container(app_: fastapi.FastAPI) -> Container:

packages/modern-di-litestar/modern_di_litestar/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
litestar_request = providers.ContextProvider(scope=Scope.REQUEST, context_type=litestar.Request)
19-
litestar_websocket = providers.ContextProvider(scope=Scope.REQUEST, context_type=litestar.WebSocket)
19+
litestar_websocket = providers.ContextProvider(scope=Scope.SESSION, context_type=litestar.WebSocket)
2020

2121

2222
def fetch_di_container(app_: litestar.Litestar) -> Container:

packages/modern-di/modern_di/container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def resolve(self, dependency_type: type[T_co] | None = None, *, dependency_name:
9191
return typing.cast(T_co, provider.resolve(self))
9292

9393
def resolve_provider(self, provider: "AbstractProvider[T_co]") -> T_co:
94-
return typing.cast(T_co, provider.resolve(self.find_container(provider.scope)))
94+
return typing.cast(T_co, provider.resolve(self))
9595

9696
async def close_async(self) -> None:
9797
if not self.parent_container:

packages/modern-di/modern_di/providers/context_provider.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ def __init__(self, *, scope: Scope = Scope.APP, context_type: type[types.T_co])
1717
self._context_type = context_type
1818

1919
def resolve(self, container: "Container") -> types.T_co | None:
20+
container = container.find_container(self.scope)
2021
return container.context_registry.find_context(self._context_type)

packages/modern-di/modern_di/providers/factory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def _compile_kwargs(self, container: "Container") -> dict[str, typing.Any]:
5959
return result
6060

6161
def resolve(self, container: "Container") -> types.T_co:
62+
container = container.find_container(self.scope)
6263
cache_item = container.cache_registry.fetch_cache_item(self)
6364
if cache_item.kwargs is not None:
6465
kwargs = cache_item.kwargs

packages/modern-di/tests_core/providers/test_singleton.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class MyGroup(Group):
3636
)
3737

3838

39-
def test_app_singleton() -> None:
39+
async def test_app_singleton() -> None:
4040
app_container = Container(groups=[MyGroup])
4141
singleton1 = app_container.resolve_provider(MyGroup.app_singleton)
4242
singleton2 = app_container.resolve_provider(MyGroup.app_singleton)
@@ -45,6 +45,9 @@ def test_app_singleton() -> None:
4545
cache_item = app_container.cache_registry.fetch_cache_item(MyGroup.app_singleton)
4646
assert cache_item.cache
4747

48+
app_container.resolve_provider(MyGroup.app_singleton)
49+
await app_container.close_async()
50+
4851

4952
async def test_request_singleton() -> None:
5053
app_container = Container(groups=[MyGroup])

0 commit comments

Comments
 (0)