python / python/mypy

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

Open
#19,622 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug performance topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the slowdown in the linked mypy Playground using the constrained TypeVar example, then compare it with the upper-bounded version. Investigate the analysis path for dataclass or dataclass_transform classes and confirm completion when many constrained variables no longer cause disproportionate analysis time.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.