python / python/mypy

False positive error "incompatible type" on bound TypeVar

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

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

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

説明

Originally posted on gitter july 29th, where it was suggested I open a bug report

Bug Report
For this code:

from typing import Generic, TypeVar, Union

ListT = TypeVar("ListT", str, float)


class TestListA(Generic[ListT]):
    def __init__(self, device: ListT) -> None:
        self._device: ListT = device


class TestListB(TestListA[ListT]):
    def __init__(self, device: ListT) -> None:
        super().__init__(device)

mypy outputs error:

test.py:13: error: Argument 1 to "__init__" of "TestListA" has incompatible type "str"; expected "ListT"  [arg-type]
test.py:13: error: Argument 1 to "__init__" of "TestListA" has incompatible type "float"; expected "ListT"  [arg-type]

which is false positive.

Please note that when using a TypeVar with bound=Union, then mypy doesn't complain.

ListT = TypeVar("ListT", bound=Union[str, float])

Your Environment

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.9.12
  • Operating system and version:

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

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

はじめの一歩

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

調査の方向性

まず、提供された TestVar の例を mypy 0.971 で再現し、次に、その制約付き TypeVar の動作を bound=Union バリアントと比較します。TestListB.init の型チェック経路を特定し、誤検出の診断に対する回帰テストのカバレッジを追加します。制約付きの例がエラーなしで通過し、互換性のない呼び出しについては引き続き診断されれば完了です。

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

評価

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

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

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