Skip to content

Commit c120a37

Browse files
Bugfix: Add enter_scope in BaseContainer.inject()
1 parent fdcb515 commit c120a37

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

tests/test_injection.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,36 @@ async def _injected(val: float = Provide()) -> float:
10401040
await _injected()
10411041

10421042

1043+
def test_inject_with_enter_scope_enters_scope_using_container_sync() -> None:
1044+
def _sync_creator() -> typing.Iterator[float]:
1045+
yield random.random()
1046+
1047+
class _Container(BaseContainer):
1048+
resource = providers.ContextResource(_sync_creator).with_config(scope=ContextScopes.INJECT)
1049+
1050+
@_Container.inject(enter_scope=True)
1051+
def _injected(val: float = Provide[_Container.resource]) -> float:
1052+
assert get_current_scope() is ContextScopes.INJECT
1053+
return val
1054+
1055+
assert isinstance(_injected(), float)
1056+
1057+
1058+
async def test_inject_with_enter_scope_enters_scope_using_container_async() -> None:
1059+
async def _async_creator() -> typing.AsyncIterator[float]:
1060+
yield random.random()
1061+
1062+
class _Container(BaseContainer):
1063+
resource = providers.ContextResource(_async_creator).with_config(scope=ContextScopes.INJECT)
1064+
1065+
@_Container.inject(enter_scope=True)
1066+
async def _injected(val: float = Provide[_Container.resource]) -> float:
1067+
assert get_current_scope() is ContextScopes.INJECT
1068+
return val
1069+
1070+
assert isinstance(await _injected(), float)
1071+
1072+
10431073
def test_inject_with_enter_scope_enters_scope_sync() -> None:
10441074
def _sync_creator() -> typing.Iterator[float]:
10451075
yield random.random()

that_depends/meta.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,21 @@ def inject(
195195
cls,
196196
*,
197197
scope: ContextScope | None = ContextScopes.INJECT,
198+
enter_scope: bool = False,
198199
) -> typing.Callable[[typing.Callable[P, T]], typing.Callable[P, T]]: ...
199200

200201
def inject(
201-
cls, func: typing.Callable[P, T] | None = None, scope: ContextScope | None = ContextScopes.INJECT
202+
cls,
203+
func: typing.Callable[P, T] | None = None,
204+
scope: ContextScope | None = ContextScopes.INJECT,
205+
enter_scope: bool = False,
202206
) -> typing.Callable[P, T] | typing.Callable[[typing.Callable[P, T]], typing.Callable[P, T]]:
203-
"""Inject dependencies into a function.
207+
"""Inject dependencies from this container into a function.
204208
205209
If your function does not use `Provide()` for dependency markers, consider using `@inject` instead.
206210
"""
207211
return (
208-
that_depends.inject(scope=scope, container=cls)
212+
that_depends.inject(scope=scope, container=cls, enter_scope=enter_scope)
209213
if func is None
210-
else that_depends.inject(scope=scope, container=cls)(func)
214+
else that_depends.inject(scope=scope, container=cls, enter_scope=enter_scope)(func)
211215
)

0 commit comments

Comments
 (0)