Severe performance degradation with lots of constrained type variables in a generic class
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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