Skip to content

Commit a7be7d3

Browse files
committed
Fix Python 3.8 warning for type comparison
1 parent c601451 commit a7be7d3

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

jmespath/visitor.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,22 @@ def _is_special_integer_case(x, y):
2929
# Also need to consider that:
3030
# >>> 0 in [True, False]
3131
# True
32-
if type(x) is int and (x == 0 or x == 1):
33-
return y is True or y is False
34-
elif type(y) is int and (y == 0 or y == 1):
35-
return x is True or x is False
32+
if _is_boolean_int(x):
33+
return isinstance(y, bool)
34+
elif _is_boolean_int(y):
35+
return isinstance(x, bool)
36+
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+
)
3648

3749

3850
def _is_comparable(x):

0 commit comments

Comments
 (0)