jmespath.py
jmespath.py copied to clipboard
Fix TypeError on ordering comparison between string and number
Ordering comparators (<, <=, >, >=) crash with TypeError when comparing a string to a number:
>>> jmespath.search('[?a < b]', [{"a": "foo", "b": 5}])
TypeError: '<' not supported between instances of 'str' and 'int'
Per the JMESPath spec, ordering comparisons on incompatible types should return null.
The bug is in _is_comparable() — it returns True for numbers or strings independently. When one operand is a string and the other is a number, both pass the individual check, but operator.lt("foo", 5) raises TypeError in Python 3.
The fix requires both operands to be the same comparable type (both numbers or both strings). This also removes the unused num_types variable that was left over from before string comparisons were added.