python / python/mypy

Pattern matching a dataclass with different field type fails for Python >= 3.13

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

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

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

説明

Bug Report

Pattern matching on a dataclass with a field of different type than required by the dataclass fails with Cannot determine type of ... for Python 3.13 and higher

To Reproduce

3.12 working gist: https://mypy-play.net/?mypy=latest&python=3.12&gist=f2947a04a9a86defefd16e54c0133c84
3.13 failing gist: https://mypy-play.net/?mypy=latest&python=3.13&gist=f2947a04a9a86defefd16e54c0133c84

from dataclasses import dataclass

@dataclass
class A:
    pass

@dataclass
class B:
    pass

@dataclass
class C:
    sth: A

def func(c: C, b: B):
    match c:
        case C(B() as obj) if obj == b:
            print("matched")

Expected Behavior

I believe it should resolve the type same as for <= 3.12.

Actual Behavior

For Python >= 3.13 inferring obj type fails with:

repro.py:17: error: Cannot determine type of "obj"  [has-type]

Your Environment

  • Mypy version used: 1.19.1
  • Mypy command-line flags: mypy repro.py
  • Mypy configuration options from mypy.ini (and other config files): no additional config
  • Python version used: 3.13.5

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

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

はじめの一歩

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

調査の方向性

repro.py の例から始め、リンクされた playground のケースを使って Python 3.12 および 3.13 上で mypy 1.19.1 を実行します。case C(B() as obj) に関係するパターンマッチングと dataclass の推論を追跡します。Python 3.13 が Cannot determine type エラーなしで 3.12 と一貫して obj を推論できれば完了です。

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

評価

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

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

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