Fix filter equality against int literal truncating float values (#227) - #241
Open
youdie006 wants to merge 1 commit into
Open
Fix filter equality against int literal truncating float values (#227)#241youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
In Expression.find (jsonpath_ng/ext/filter.py), when the RHS literal is an int, the LHS value was coerced with int(value) before comparison. For a float LHS this truncates (int(0.6) == 0), so an equality filter against 0 wrongly matched 0.6, e.g. $[?(@.v = 0)] matched an element with v = 0.6. Restrict the coercion to string LHS values (isinstance(value, str)). This preserves the original string->int intent (matching "5" against 5) while leaving numeric values uncoerced, so float comparisons stay exact. Booleans are unaffected (type(True) is int is False). Fixes h2non#227.
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 #227.
Problem
An extended filter comparing against an integer literal wrongly matched float values. For example
$[?(@.v = 0)]matched an element withv = 0.6:Root cause
In
Expression.find(jsonpath_ng/ext/filter.py), when the RHS literal is anint, the LHS value was coerced withint(value)before comparison. For a float LHS this truncates (int(0.6) == 0), so an equality filter against0wrongly matched0.6.Fix
Restrict the coercion to string LHS values (
isinstance(value, str)). This preserves the original string->int intent (e.g. matching"5"against5) while leaving numeric values uncoerced, so float comparisons stay exact. Booleans are unaffected (type(True) is intisFalse).Tests
issue-227-int-no-float-truncation.["A", "B", "C"](fails); with the fix it returns["A", "C"].tests/pass (352 passed).[Unreleased] / Fixedper CONTRIBUTING.md.Thanks to @AkiraVoid for the clear bug report.
This change was prepared with AI assistance and reviewed by me before submission.