python / python/mypy

Bad error message while checking subclass of generic classes

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

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

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

説明

Bug Report

mypy command on a misspecified subclass of a generic class returns a pretty confusing error message (As per issue #8696)

To Reproduce

  1. Create a script containing this snippet:
from abc import ABC, abstractmethod
from typing import TypeVar, Iterable, Iterator

T = TypeVar('T')


class MyABC(ABC):

    @property
    @abstractmethod
    def items(self) -> Iterable[T]: ...

    def my_method(self) -> Iterator[T]:
        for item in self.items:
            yield item
  1. Run mypy on the created script

Expected Behavior

Since MyABC should inherit also from Generic[T] I'd expect that the error message would return something on the line of:

error: Incompatible types in "yield". The class should inherit from "Generic[T]"

Actual Behavior

The actual error message is:

error: Incompatible types in "yield" (actual type "T", expected type "T")

Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.6.5
  • Operating system and version: MacOS BigSur 11.5.2

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

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

はじめの一歩

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

調査の方向性

まず issue 内の最小スクリプトに対して mypy を実行し、紛らわしい yield の診断を確認します。リポジトリのファイル名やテスト名は示されていないため、mypy コマンドのエントリポイントから診断を追跡し、その後、誤って指定されたジェネリックサブクラスが、欠落している Generic[T] の継承を特定する実用的なメッセージを生成することを確認します。

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

評価

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

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

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