Fix TypeError when ordering-comparing a number against a string (#169)#364
Open
apoorvdarshan wants to merge 1 commit into
Open
Conversation
Ordering operators (`<`, `<=`, `>`, `>=`) are only valid when both operands are in the same comparable category. `_is_comparable` was applied to each operand independently, so a number and a string both passed the guard and were then compared directly, raising `TypeError: '<' not supported between instances of 'int' and 'str'` instead of yielding null. Require both operands to be numbers or both to be strings before comparing; otherwise return None, matching the spec and the behavior already used for other incomparable types. Fixes jmespath#169
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 #169
Root cause
Ordering operators (
<,<=,>,>=) are only valid when both operands belong to the same comparable category (both numbers, or both strings). InTreeInterpreter.visit_comparator, the guard applied_is_comparableto each operand independently:Since
_is_comparable(x)returnedTruefor either a number or a string, a number/string pair (e.g.1and"2") passed the guard and was then handed straight tooperator.lt, which raisesTypeErrorin Python 3.Reproduction
Actual output on
develop(also reported on 0.9.3 / 1.0.1):Per the spec, an ordering comparison between incomparable types evaluates to
null, so the filter should simply exclude the item. Corrected output:Same-category comparisons are unaffected:
[?id < `2`]still returns[{"id": 1}], and[?name < 'm']still works for strings.Fix
_is_comparablenow takes both operands and requires them to be in the same category before an ordering comparison is attempted:The call site becomes
_is_comparable(left, right). When the operands are not in the same category,visit_comparatorreturnsNone, matching the behavior already used for other incomparable types (list, null, bool). The change is confined tovisit_comparatorand its private helper; the now-unused localnum_types = (int, float)on the adjacent line was removed._is_comparableis a private, module-internal helper with no other callers.Tests
Added regression cases to
tests/compliance/boolean.jsonalongside the existing incomparable-ordering cases (emptylist < one,one < boolvalue, ...), covering a number vs. string in both directions for every ordering operator:These fail on the unmodified code (with the
TypeErrorabove) and pass after the fix. The full test suite passes (996 passed, 1 skipped).Disclosure: prepared with AI assistance; reviewed and verified locally.