fix(api): preserve precision for large integers in safe_int#35115
Open
HamzaSwitch wants to merge 1 commit intolanggenius:mainfrom
Open
fix(api): preserve precision for large integers in safe_int#35115HamzaSwitch wants to merge 1 commit intolanggenius:mainfrom
HamzaSwitch wants to merge 1 commit intolanggenius:mainfrom
Conversation
safe_int() was converting through float first via int(float(value)),
which silently truncates integers with more than ~15 significant digits
because float64 cannot represent them exactly. For example:
int(float('25227143063332189585438')) yields 25227143063332188061696,
losing the last 8 digits.
Try int(value) first to get arbitrary-precision integer conversion for
integer-like strings. Fall back to int(float(value)) only if int()
fails, which handles float-like strings such as '3.14' or '1e10'.
Fixes langgenius#34405
Contributor
Pyrefly DiffNo changes detected. |
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 #34405.
safe_int()was converting throughfloatfirst viaint(float(value)), which silently truncates integers with more than ~15 significant digits because float64 can't represent them exactly. For example:int(float('25227143063332189585438'))yields25227143063332188061696, losing the last 8 digits. The reporter saw this when passing large integers through code nodes and MCP tool nodes.Fix: try
int(value)first for arbitrary-precision integer conversion. Fall back toint(float(value))only whenint()fails, which still handles float-like strings like"3.14"or"1e10".Verified: large int
25227143063332189585438round-trips correctly, normal ints, float strings, None, "null", empty string, and garbage inputs all behave as before. Ruff clean.