python / python/mypy

Unclear message if named tuple is not compatible with protocol

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

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

diagnostics priority-1-normal topic-error-reporting topic-named-tuple topic-protocols topic-usability
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Mypy just says that class is not compatible with the bound when type checking this example:

from typing import Protocol, Generic, TypeVar, NamedTuple, List, Any

class Example(Protocol):
    sentence: List[str]

T = TypeVar('T', bound=Example)

class Dataset(Generic[T]):
    ...

class ConcreteExample(NamedTuple):
    sentence: List[str]
    others: Any

class ConcreteDataset(Dataset[ConcreteExample]):
    ...

Here's the error message:

t.py:15: error: Type argument "Tuple[builtins.list[builtins.str], Any, fallback=t.ConcreteExample]" of "Dataset" must be a subtype of "t.Example"

It would be better to also say that sentence in ConcreteExample is read-only, and maybe suggest making it a read-only property in the protocol.

Originally reported in #7212.

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

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

はじめの一歩

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

調査の方向性

まず issue の例を再現し、NamedTuple 型に対する現在の bound error を確認します。ConcreteExample.sentence が読み取り専用であることを説明し、protocol に読み取り専用の property を提案できるように診断を更新します。適切な型チェックテストで更新後のメッセージを検証します。

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

評価

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

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

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