Skip to content

Avoid exponential expansion of unused value-restricted type variables - #22299

Open
MustafaKemal0146 wants to merge 1 commit into
python:masterfrom
MustafaKemal0146:fix-overload-typevar-explosion
Open

MustafaKemal0146 wants to merge 1 commit into
python:masterfrom
MustafaKemal0146:fix-overload-typevar-explosion

Conversation

@MustafaKemal0146

Copy link
Copy Markdown

Fixes #22257

Code generated by mypy-protobuf defines stub classes generic in many value-restricted type variables, with overloaded __init__ methods that use fully concrete explicit self types. mypy expanded every value combination of all class type variables in two places, even though none of them appear in these signatures, causing exponential time and a MemoryError crash (2^34 variants in the reported case):

  • expand_callable_variants() (unsafe overload overlap check): detach_callable() adds all class type variables, and all their combinations were expanded. We now only expand type variables that appear in the callable (including bounds/values of its own variables and TypeGuard/TypeIs). Substituting an unused type variable is a no-op, so this only removes duplicate variants.
  • TypeChecker.expand_typevars(): for functions with a trivial body we now only expand type variables that appear in the signature, since others can't affect the result. Non-trivial bodies are expanded as before.

This doesn't address cases where many value-restricted type variables actually appear in a signature (see #20439).

Tests: two new regression cases (20 type variables; they time out without the fix and take ~1 s with it), including an overlapping overload pair to check the error is still reported and a non-trivial body that is still expanded. The original repro from the issue now passes in ~3 s.

🤖 Generated with Claude Code

Stubs generated by mypy-protobuf define classes generic in many
value-restricted type variables, with overloaded methods that use fully
concrete explicit self types. expand_callable_variants() and
TypeChecker.expand_typevars() expanded every value combination of all
class type variables, even ones not used in the signature, causing
exponential time and a MemoryError crash.

Only expand type variables that appear in the callable in
expand_callable_variants(), and in the signature for functions with a
trivial body in expand_typevars().

Fixes python#22257

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 26, 2026 11:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants

⌂ Proxy