python / python/mypy

Type inference problem when tying a class with a type var

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

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

bug topic-type-variables
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Please consider

import dataclasses as dtc
import typing as tp

T = tp.TypeVar('T', int, str, covariant=True)

@dtc.dataclass(frozen=True)
class C(tp.Generic[T]):
    c: type[T]

c_decision: dict[bool, C] = {
    True: C(int),
    False: C(str),
}

reveal_type(c_decision[True])
reveal_type(c_decision[False])

then

$ mypy -V
mypy 0.942
$ mypy issues-mypy/t9.py
issues-mypy/t9c.py:15: note: Revealed type is "t9c.C*[Any]"
issues-mypy/t9c.py:16: note: Revealed type is "t9c.C*[Any]"

If I remove type annotation for c_decision

c_decision = {
    True: C(int),
    False: C(str),
}

then the output is

issues-mypy/t9c.py:15: note: Revealed type is "builtins.object*"
issues-mypy/t9c.py:16: note: Revealed type is "builtins.object*"

EDIT: rewriting the description to try to simplify the problem

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

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

はじめの一歩

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

調査の方向性

まず、示されている再現コード issues-mypy/t9.py、または参照されているバリアント t9c.py に対して mypy を実行し、c_decision のアノテーションがある場合とない場合の表示された型を比較します。C(int) と C(str) の推論を追跡します。意図した動作が回帰テストで捉えられたら完了です。

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

評価

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

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

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