Skip to content

Commit e6bc868

Browse files
authored
Merge pull request #96 from fhdufhdu/master
Added context manager condition to attr function(Fix #94)
2 parents 80f1948 + 189c20d commit e6bc868

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/inject/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,14 @@ def __init__(self, cls: Binding) -> None:
281281
self._cls = cls
282282

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

286293

287294
class _AttributeInjectionDataclass(Generic[T]):

0 commit comments

Comments
 (0)