mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Internal error occurs at @typing.overload in large protoc generated files

Open mjohanse-emr opened this issue 1 month ago • 4 comments

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

Image

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

mjohanse-emr avatar Oct 21 '26 17:10 mjohanse-emr

I've attached source code to reproduce this.

I'm not seeing the attached source code on the issue?

EmmanuelNiyonshuti avatar Oct 21 '26 17:10 EmmanuelNiyonshuti

Adding attachment that I forgot on creation.

mypy-bug-repro.zip

mjohanse-emr avatar Oct 21 '26 18:10 mjohanse-emr

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.

mjohanse-emr avatar Oct 21 '26 18:10 mjohanse-emr

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.

MustafaKemal0146 avatar Oct 26 '26 11:10 MustafaKemal0146