Skip to content

Commit 51dc960

Browse files
authored
Merge pull request #126 from jamesls/fix-string-ordering
Add back support for order comparisons against strings
2 parents de03509 + b893fc2 commit 51dc960

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

jmespath/visitor.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import operator
22

33
from jmespath import functions
4+
from jmespath.compat import string_type
45

56

67
def _equals(x, y):
@@ -33,6 +34,14 @@ def _is_special_integer_case(x, y):
3334
return x is True or x is False
3435

3536

37+
def _is_comparable(x):
38+
# The spec doesn't officially support string types yet,
39+
# but enough people are relying on this behavior that
40+
# it's been added back. This should eventually become
41+
# part of the official spec.
42+
return _is_actual_number(x) or isinstance(x, string_type)
43+
44+
3645
def _is_actual_number(x):
3746
# We need to handle python's quirkiness with booleans,
3847
# specifically:
@@ -142,8 +151,8 @@ def visit_comparator(self, node, value):
142151
left = self.visit(node['children'][0], value)
143152
right = self.visit(node['children'][1], value)
144153
num_types = (int, float)
145-
if not (_is_actual_number(left) and
146-
_is_actual_number(right)):
154+
if not (_is_comparable(left) and
155+
_is_comparable(right)):
147156
return None
148157
return comparator_func(left, right)
149158

tests/test_search.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ def _func_my_subtract(self, x, y):
3636
jmespath.search('my_subtract(`10`, `3`)', {}, options=options),
3737
7
3838
)
39+
40+
41+
class TestPythonSpecificCases(unittest.TestCase):
42+
def test_can_compare_strings(self):
43+
# This is python specific behavior that's not in the official spec
44+
# yet, but this was regression from 0.9.0 so it's been added back.
45+
self.assertTrue(jmespath.search('a < b', {'a': '2016', 'b': '2017'}))

0 commit comments

Comments
 (0)