Skip to content

Commit

Permalink
added context manager condition to attr function
Browse files Browse the repository at this point in the history
  • Loading branch information
fhdufhdu committed Mar 22, 2024
1 parent 122c551 commit fd903db
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/inject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def my_config(binder):
inject.configure(my_config)
"""
import asyncio
import contextlib

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

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


class _AttributeInjectionDataclass(Generic[T]):
Expand Down

0 comments on commit fd903db

Please sign in to comment.