dataclass collection inference problem
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
It seems that when typing a collection of dataclasses, if they all happen to have the same fields, then the inferred type will be a Callable instead of type.
The example below is self-contained and demonstrates the problem very clearly.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.14&gist=5dfadfda23e846913cbee4d56568b69e
from dataclasses import Field, dataclass
from typing import ClassVar, Protocol, TypeVar
@dataclass
class C1:
foo: int = 0
@dataclass
class C2:
foo: int = 1
@dataclass
class C3:
foo: int = 2
bar: str = ""
class Dataclass(Protocol):
__dataclass_fields__: ClassVar[dict[str, Field]]
DataclassT = TypeVar("DataclassT", bound=Dataclass)
def f(a1: list[type[DataclassT]]): ...
reveal_type([C1, C2])
reveal_type([C1, C3])
f([C1, C2]) # this gives an error, but the others work
f([C1, C3])
f([C2, C3])
f([C1, C2, C3])
Expected Behavior
I would expect dataclasses to always be typed as classes instead of becoming callables because they happen share fields.
Actual Behavior
t.py:31: note: Revealed type is "builtins.list[def (foo: builtins.int =) -> builtins.object]"
t.py:32: note: Revealed type is "builtins.list[builtins.type]"
t.py:34: error: Value of type variable "DataclassT" of "f" cannot be "object" [type-var]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.18.2 (but I tested 1.18.1, 1.17.1, 1.17.0 and they all have the same problem)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): follow_untyped_imports = true - Python version used: 3.14.0 (also fails with 3.13.*)
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Issue 内の自己完結型の再現コードと、リンクされている mypy-play の例から始め、dataclass クラスを含むリストリテラルに対する mypy の型推論を追跡してください。完了条件は、[C1, C2] が callable ではなくクラス型のリストとして推論され、示されている f の呼び出しがすべて type-variable エラーなしで型チェックを通ることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100