python / python/mypy

Unexpected incompatible types in assignment (Failed in recursive type checking, nested protocol with generic type)

オープン
#16,612 コメント 9 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report
Here I write some nested protocol with generic type.

The basic protocol classes with generic type succeed to match the classes written in the pyi file, like this:

t: Tempo[int] = core.TempoTick(0, 0) #  pass
t: Temop = core.TemoTick(0,0)        #  pass

However, for upper layer class, list List[Tempo], it just started to fail (with generic), like this:

Note that List is a self defined protocol here for classes generated by pybind11 bind_vector

tempos2: List[Tempo] = core.TempoTickList()     #  not pass
tempos3: List = core.TempoTickList()            #  pass

图片

The full errors are listed here:

Incompatible types in assignment (expression has type "TempoTickList", variable has type "symusic.types.List[Tempo[Any]]")Mypy[assignment](https://mypy.readthedocs.io/en/latest/_refs.html#code-assignment)
Following member(s) of "TempoTickList" have conflicts:
    Expected:
        @overload
        def __setitem__(self, int, Tempo[Any], /) -> None
        @overload
        def __setitem__(self, slice, TempoTickList, /) -> None
    Got:
        @overload
        def __setitem__(self, int, TempoTick, /) -> None
        @overload
        def __setitem__(self, slice, TempoTickList, /) -> None
    Expected:
        def append(self, x: Tempo[Any]) -> None
    Got:
        def append(self, x: TempoTick) -> None
    <1 more conflict(s) not shown>Mypy

Although Tempo and Tempo[int] have matched TempoTick, they are said failed to match in the errors.

Even if I remove some of the nested generic type like below, it still failed:

class Score(Protocol[TimeDtype]):
    ticks_per_quarter: int
    tracks: List
    key_signatures: List
    time_signatures: List
    tempos: List
    lyrics: List
    markers: List

图片

By the way, in pycharm, it says, all of these classes fail to match, quite different from mypy. And different from mypy, pycharm don't offer me any useful debug information. (But pycharm works as expected in my following test while mypy failed. So I guess there are at least 2 problems in total.)

图片

To Reproduce
I put my codes here (just a few files):
symusic.zip

And the code in picture is at the bottom of the types.py. I just use the vscode MyPy Type Checker plugin.

Expected Behavior

Well, I just expect it to match.

Actual Behavior

They don't match when the protocol with generic type is nested

Your Environment

  • Mypy version used: mypy 1.7.1 (compiled: yes)
  • Mypy command-line flags: default in vscode
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.11.6

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

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

はじめの一歩

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

調査の方向性

添付された再現用の symusic.zip、特に types.py の末尾にあるコードから始め、デフォルト設定を使って mypy 1.7.1 で実行してください。ネストされたジェネリックプロトコルの代入を報告されたエラーと比較してください。TempoTickList と関連するプロトコルの代入が、より単純なケースと一貫して受け入れられれば完了です。

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

評価

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

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

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