Internal error occurs at @typing.overload in large protoc generated files
Crash Report
When running mypy on some large protoc generated python files, it consistently returns an internal error at the same line. Unfortunately, after mypy returns this internal error, I cannot copy the contents of the command prompt. I will paste a screenshot of the error report and traceback in the traceback section.
Traceback
To Reproduce
I've attached source code to reproduce this. I was using poetry to build and run mypy, so the steps I performed are:
- Open command prompt to the package root
- Run
poetry build && poetry install - Run
poetry run mypy - See mypy take a long time and eventually fail with an internal error.
Your Environment
- Mypy version used: 2.4.0
- Mypy command-line flags: --show-traceback
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.13.15
- Operating system and version: Windows 11
I've attached source code to reproduce this.
I'm not seeing the attached source code on the issue?
2. > I've attached source code to reproduce this.
I'm not seeing the attached source code on the issue?
@EmmanuelNiyonshuti sorry about that, I forgot to attach when I created. I just commented with the attachment.
Thanks for the repro. The crash is a MemoryError from a combinatorial explosion. MetadataStoreServiceStub is generic in 34 value-restricted TypeVars (2 values each). When checking the __init__ overloads, mypy expands every combination of those class TypeVars (2^34) in expand_callable_variants(), even though the overload signatures (explicit concrete self: types) don't use any of them. The same happens again when the overload items are type checked (TypeChecker.expand_typevars()).
Minimal repro (as a .pyi; with ~16 TypeVars it already takes minutes on master):
from typing import Generic, TypeVar, overload
T0 = TypeVar("T0", int, str)
... # T1..T33 the same
class C(Generic[T0, ..., T33]):
@overload
def __init__(self: C[int, ..., int], x: int) -> None: ...
@overload
def __init__(self: C[str, ..., str], x: str) -> None: ...
Skipping type variables that don't appear in the signature fixes it without changing semantics (substituting an unused type variable is a no-op). I'll open a PR. Related: #20439, nipunn1313/mypy-protobuf#707.