Closed trollfot closed 7 months ago
hm... I feel silly, since this seems to be tested and implemented. Yet, in my code, it does not work.
I confirm that the "type_" returned is not correct :
collections.abc.Iterator[sqlmodel.orm.session.Session]
It seems that my function :
@contextmanager
def sqlsession(self) -> Iterator[Session]:
with Session(self.engine) as session:
try:
yield session
except Exception:
# maybe log.
raise
else:
session.commit()
does not qualify as inspect.isgeneratorfunction
Ok, it seems I found the problem. I was using the decorator "@beartype" on the class, that also wraps every method. When checking for the wrapped function, the code only unwraps one level.
I don't know if that's a common usecase, that we could be providing factories decorated multiple times.
The function _guess_return_type
could be using inspect.unwrap
to recover the canonical function, instead of
maybe_wrapped = getattr( # @functools.wraps
factory,
"__wrapped__",
factory,
)
I'll take a look
Let me know if I can provide some assistance or if I need to write a test to demonstrate the usecase !
If you don't mind installing from git again:
git+https://github.com/ThirVondukr/aioinject.git@b446776ad9d74ab5fb9c3af6170e8c5b7a24a734
I just tested it on a function with multiple decorators, if everything is ok I could make a release
tested and working !
Currently, to allow a proper discovery of the type, the provider uses the direct annotation. In case of Context Managers, this is a bit hairy, as they should be
collections.abc.Iterator
orcollections.abc.Generator
. Should we try to make this work ?