Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back support for order comparisons against strings #126

Merged
merged 1 commit into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions jmespath/visitor.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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'}))