Do not crash when folding constants that overflow - #21820
Open
Eljees wants to merge 1 commit into
Open
Conversation
This comment has been minimized.
This comment has been minimized.
Eljees
force-pushed
the
fix/17008-17534-constant-fold-overflow
branch
from
August 8, 2026 06:39
87fe25b to
d871b03
Compare
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 #17008.
Fixes #17534.
These turned out to be two ends of the same pipe, so they are fixed together.
Folding overflows (#17008).
constant_fold_binary_int_opandconstant_fold_binary_float_optake unbounded ints, and several operations cannot produce a result for large operands. Calling them directly on currentmaster:The
**branches already returnNonein exactly this situation, so the repository was answering its own question — folding is an optimization, and when it cannot produce a value the expression should simply stand. The other seven paths now do the same.value_repr(#17534).repr()of an int is bounded bysys.set_int_max_str_digits(), so aLiteralTypebuilt from a folded power raisedValueError: Exceeds the limit (4300 digits). It now falls back tohex(), which is lossless and unbounded. This is @devdanzin's suggestion from the thread.The two are connected:
int **builds the huge value that later explodes invalue_repr. Guarding folding alone would leave literals that come from other paths, so both sides are handled.Verified: the four new tests fail on the unpatched tree with exactly the exceptions above, and pass with the change.
mypy/test/testtypes.pygoes 119 → 120 passed (2 skipped) with no other movement.blackandruff checkclean.Small operands are covered by a test to pin that folding still happens (
6 / 3 == 2.0,1 << 4 == 16,2.0 ** 3 == 8.0).AI-assisted (LLM used for drafting); the reproductions and runs above are mine.