Overloads in generic classes are extremely slow when using generic self
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Performance of mypy degrades drastically with the number of type variable constraints when checking overloaded method of generic class with overloaded self.
To Reproduce
from typing import TypeVar, Generic, TypeAlias, overload, Any
T1 = TypeVar('T1', int, str, bool)
T2 = TypeVar('T2', int, str, bool)
T3 = TypeVar('T3', int, str, bool)
T4 = TypeVar('T4', int, str, bool)
T5 = TypeVar('T5', int, str, bool)
T6 = TypeVar('T6', int, str, bool)
T7 = TypeVar('T7', int, str, bool)
class A(Generic[T1, T2, T3, T4, T5, T6, T7]):
@overload
def foo(self: A[int, T2, T3, T4, T5, T6, T7]) -> int:
...
@overload
def foo(self: A[str, T2, T3, T4, T5, T6, T7]) -> str:
...
def foo(self) -> int | str:
raise NotImplementedError
https://mypy-play.net/?mypy=latest&python=3.12&gist=fd939322936206cbb9b1f45a71fbd9b7
Expected Behavior
Code typechecks in realistic time. For reference pyrights checks such code without significant performance issues
Actual Behavior
mypy-play.net times out. In my experience it takes at least more than 10 minutes to check such code. Interestingly, when converting foo method to an independent function taking A as a parameter typechecking time drops significantly.
Your Environment
python3.11 and python3.12, both mypy 1.12.1 and 1.13
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
Start with the supplied Python reproduction and mypy-play link, then compare its checking time with the independent-function variant described in the report. Measure how runtime changes as the number of constrained type variables grows, and use the expected behavior—typechecking in realistic time—as the completion criterion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100