Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add back support for order comparisons against strings
The validation added in 0.9.1 to make this compliant with the
spec actually broke a number of people.  I've added this back
and will work on getting the spec updated.

Fixes jmespath#124.
  • Loading branch information
jamesls committed Mar 10, 2017
1 parent de03509 commit b893fc2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 11 additions & 2 deletions jmespath/visitor.py
@@ -1,6 +1,7 @@
import operator

from jmespath import functions
from jmespath.compat import string_type


def _equals(x, y):
Expand Down Expand Up @@ -33,6 +34,14 @@ def _is_special_integer_case(x, y):
return x is True or x is False


def _is_comparable(x):
# The spec doesn't officially support string types yet,
# but enough people are relying on this behavior that
# it's been added back. This should eventually become
# part of the official spec.
return _is_actual_number(x) or isinstance(x, string_type)


def _is_actual_number(x):
# We need to handle python's quirkiness with booleans,
# specifically:
Expand Down Expand Up @@ -142,8 +151,8 @@ def visit_comparator(self, node, value):
left = self.visit(node['children'][0], value)
right = self.visit(node['children'][1], value)
num_types = (int, float)
if not (_is_actual_number(left) and
_is_actual_number(right)):
if not (_is_comparable(left) and
_is_comparable(right)):
return None
return comparator_func(left, right)

Expand Down
7 changes: 7 additions & 0 deletions tests/test_search.py
Expand Up @@ -36,3 +36,10 @@ def _func_my_subtract(self, x, y):
jmespath.search('my_subtract(`10`, `3`)', {}, options=options),
7
)


class TestPythonSpecificCases(unittest.TestCase):
def test_can_compare_strings(self):
# This is python specific behavior that's not in the official spec
# yet, but this was regression from 0.9.0 so it's been added back.
self.assertTrue(jmespath.search('a < b', {'a': '2016', 'b': '2017'}))

0 comments on commit b893fc2

Please sign in to comment.