python / python/mypy

Mypy treating type of final class variable in dataclass as Any

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

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

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

説明

Bug Report

from dataclasses import dataclass
from typing import ClassVar, Final, reveal_type

class Bar: pass

@dataclass
class Foo:
    cv: ClassVar[Final[dict[type[Bar], Foo]]] = {}

reveal_type(Foo.cv)
$ mypy test.py
test.py:10: note: Revealed type is "Any"
Success: no issues found in 1 source file

The correct type of Foo.cv is builtins.dict[type[Bar], Foo].

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.14&gist=2fad4ef5fded90ecaeb0f1d1e59d882a

Expected Behavior

reveal_type should report the correct type for the class variable.

Actual Behavior

reveal_type showed incorrect type (Any).

Your Environment

  • Fedora 44
  • python3-mypy-1.18.2-4.fc44.noarch (reproducible with Mypy 2.3.1)
  • python3-3.14.6-1.fc44.x86_64
  • No config files used

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

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

はじめの一歩

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

調査の方向性

issue にある最小の再現例、またはその mypy-play リンクから始め、reveal_type の実行時に dataclass の処理が ClassVar[Final[...]] をどのように扱うかを追跡します。mypy が Foo.cv について dict[type[Bar], Foo] を報告し、その挙動が回帰テストでカバーされれば完了です。

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

評価

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

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

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