pycharm中Asyncconcontextmanager的类型提示

发布于 2025-01-25 05:47:01 字数 932 浏览 0 评论 0原文

我正在使用Pycharm Professional 2022.1。 我有一种异步方法在异步环境中使用数据库来管理会话

@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()

,但是当我在异步语句中使用此方法时,Pycharm无法识别类型:

net/5zozl.png“ rel =“ nofollow noreferrer”>

”在此处输入图像描述”

那么,您是否有任何技巧可以键入提示a的输出变量,而无需手动指定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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文