Skip to content

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

@bzoracler

Description

@bzoracler

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions