Skip to content

Commit fd903db

Browse files
committed
added context manager condition to attr function
1 parent 122c551 commit fd903db

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/inject/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def my_config(binder):
7373
inject.configure(my_config)
7474
7575
"""
76+
import asyncio
7677
import contextlib
7778

7879
from inject._version import __version__
@@ -281,7 +282,14 @@ def __init__(self, cls: Binding) -> None:
281282
self._cls = cls
282283

283284
def __get__(self, obj: Any, owner: Any) -> Injectable:
284-
return instance(self._cls)
285+
inst = instance(self._cls)
286+
if isinstance(inst, contextlib._AsyncGeneratorContextManager):
287+
raise InjectorException(
288+
'Fail to load _AsyncGeneratorContextManager, Use autoparams, param or params instead of attr funcion')
289+
elif isinstance(inst, contextlib._GeneratorContextManager):
290+
with contextlib.ExitStack() as sync_stack:
291+
inst = sync_stack.enter_context(inst)
292+
return inst
285293

286294

287295
class _AttributeInjectionDataclass(Generic[T]):

0 commit comments

Comments
 (0)