Repository navigation
Avoid exponential expansion of unused value-restricted type variables - #22299
Open
MustafaKemal0146 wants to merge 1 commit into
Open
MustafaKemal0146 wants to merge 1 commit into
MustafaKemal0146 wants to merge 1 commit into
Conversation
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>
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 explicitselftypes. 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 aMemoryErrorcrash (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 andTypeGuard/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