Skip to content

Commit f24f04b

Browse files
committed
Add support for proper bool/float comparison
1 parent a7be7d3 commit f24f04b

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

jmespath/visitor.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77

88
def _equals(x, y):
9-
if _is_special_integer_case(x, y):
9+
if _is_special_number_case(x, y):
1010
return False
1111
else:
1212
return x == y
1313

1414

15-
def _is_special_integer_case(x, y):
15+
def _is_special_number_case(x, y):
1616
# We need to special case comparing 0 or 1 to
1717
# True/False. While normally comparing any
1818
# integer other than 0/1 to True/False will always
@@ -29,23 +29,11 @@ def _is_special_integer_case(x, y):
2929
# Also need to consider that:
3030
# >>> 0 in [True, False]
3131
# True
32-
if _is_boolean_int(x):
32+
if _is_actual_number(x) and x in (0, 1):
3333
return isinstance(y, bool)
34-
elif _is_boolean_int(y):
34+
elif _is_actual_number(y) and y in (0, 1):
3535
return isinstance(x, bool)
3636

37-
def _is_boolean_int(num):
38-
"""
39-
For backwards compatibility, Python considers bool to be an int.
40-
This causes issues when doing type comparison with isinstance(x, int).
41-
This function ensures the value is an actual int respresenting a boolean.
42-
"""
43-
return (
44-
not isinstance(num, bool)
45-
and isinstance(num, int)
46-
and num in (0, 1)
47-
)
48-
4937

5038
def _is_comparable(x):
5139
# The spec doesn't officially support string types yet,
@@ -63,7 +51,7 @@ def _is_actual_number(x):
6351
# True
6452
# >>> isinstance(True, int)
6553
# True
66-
if x is True or x is False:
54+
if isinstance(x, bool):
6755
return False
6856
return isinstance(x, Number)
6957

@@ -269,7 +257,7 @@ def visit_and_expression(self, node, value):
269257

270258
def visit_not_expression(self, node, value):
271259
original_result = self.visit(node['children'][0], value)
272-
if type(original_result) is int and original_result == 0:
260+
if _is_actual_number(original_result) and original_result == 0:
273261
# Special case for 0, !0 should be false, not true.
274262
# 0 is not a special cased integer in jmespath.
275263
return False

0 commit comments

Comments
 (0)