python / python/mypy

Severe performance degradation with lots of constrained type variables in a generic class

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

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

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

説明

Bug Report, To Reproduce, and Actual Behaviour

Mypy runs noticeably slower when analysing a class created from @dataclass or @dataclass_transform(), if the class is parameterised by constrained type variables. The increase in analysis time itself increases the more constrained type variables are added.

Upper-bounded type variables don't suffer this problem.

See mypy Playground, which fails with exit code 500. It isn't necessary to add fields on the class body, although it hits the performance problem with less type variables if you do. The snippet is roughly:

from typing_extensions import TypeVar, Generic
from dataclasses import dataclass

# Constrained
T1 = TypeVar("T1", int, None)
T2 = TypeVar("T2", int, None)
...
T20 = TypeVar("T20", int, None)

@dataclass
class A(Generic[T1, T2, ..., T20]):
    pass

Expected Behavior

Is it sensible to expect performance to be similar to upper-bounded type variables?

Your Environment

  • Mypy version used: 1.17.1
  • Python version used: 3.11, 3.12

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

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

はじめの一歩

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

調査の方向性

リンクされた mypy Playground で制約付き TypeVar の例を使って低速化を再現し、その後、上限付きのバージョンと比較します。dataclass または dataclass_transform クラスの解析経路を調査し、多数の制約付き変数によって解析時間が不釣り合いに長くならなくなったことを確認して、完了とします。

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

評価

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

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

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