python / python/mypy

Error with Protocols and AsyncContextManager

オープン
#8,276 コメント 10 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

documentation needs discussion topic-usability
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

  • Are you reporting a bug, or opening a feature request?
    A bug

  • Please insert below the code you are checking with mypy,

from __future__ import annotations

import asyncio
import typing as t
from contextlib import asynccontextmanager

import typing_extensions as te


class LockProto(te.Protocol):
    async def with_lock(self) -> t.AsyncContextManager[str]: ...


class Consumer:
    def __init__(self, locker: LockProto) -> None:
        self.locker = locker

    def run(self) -> None:
        async with self.locker.with_lock() as name:
            print(name)


class SockLock:
    @asynccontextmanager
    async def with_lock(self) -> t.AsyncGenerator[str, None]:
        yield 'some-string'


async def main() -> None:
    cons = Consumer(SockLock())
    await cons.run()


if __name__ == '__main__':
    asyncio.run(main())
  • What is the actual behavior/output?
bug.py:18: error: "Coroutine[Any, Any, AsyncContextManager[str]]" has no attribute "__enter__"
bug.py:18: error: "Coroutine[Any, Any, AsyncContextManager[str]]" has no attribute "__exit__"
bug.py:29: error: Argument 1 to "Consumer" has incompatible type "SockLock"; expected "LockProto"
bug.py:29: note: Following member(s) of "SockLock" have conflicts:
bug.py:29: note:     Expected:
bug.py:29: note:         def with_lock(self) -> Coroutine[Any, Any, AsyncContextManager[str]]
bug.py:29: note:     Got:
bug.py:29: note:         def with_lock(*Any, **Any) -> AsyncContextManager[Any]
  • What is the behavior/output you expect?
    No errors

  • What are the versions of mypy and Python you are using?
    0.761

  • What are the mypy flags you are using? (For example --strict-optional)
    No flags, just mypy bug.py

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供された例を bug.py として保存し、報告されたバージョンでフラグを付けずに mypy bug.py を実行します。mypy が Protocol メソッド、非同期の戻り値の型、非同期コンテキストマネージャーをどのようにチェックするかを追跡します。例でエラーが発生せず、期待される型チェックの動作が維持されていれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。