'Type hint of AsyncContextManager in PyCharm

I'm using Pycharm Professional 2022.1. I have an async method I use to manage the session with my database in asynchronous environments

@contextlib.asynccontextmanager
async def begin_transaction(self) -> AsyncIterator[Session]:
    _session: Optional[Session] = None
    try:
        await self.session_lock.acquire()
        _session: Session = Session()
        yield _session
    finally:
        if _session is not None:
            _session.commit()
            _session.close()

        if self.session_lock.locked():
            self.session_lock.release()

But, when I use this method in an async statement, Pycharm doesn't recognize the type :

enter image description here

To use PyCharm auto-completion, I have to manually specify the type of the Session :

enter image description here

So, do you have any trick to type hint the output variable of a with statement on PyCharm without having to manually specify it ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source