From b8810723e7d3f5d3d296ee529f2b8131bf56b394 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 14:45:30 +0300 Subject: [PATCH] Fix TypeError on ordering comparison between string and number Ordering comparators (<, <=, >, >=) check each operand individually via _is_comparable(), which returns True for numbers OR strings. When one operand is a string and the other is a number, both pass the check independently but operator.lt('foo', 5) raises TypeError in Python 3. The JMESPath spec says ordering comparisons on incompatible types should return null. Fix by requiring both operands to be the same comparable type (both numbers or both strings). This also removes the unused num_types variable that was left over from an earlier version of this code. --- jmespath/visitor.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/jmespath/visitor.py b/jmespath/visitor.py index 15fb177..601747f 100644 --- a/jmespath/visitor.py +++ b/jmespath/visitor.py @@ -151,9 +151,10 @@ def visit_comparator(self, node, value): # will yield a None value. left = self.visit(node['children'][0], value) right = self.visit(node['children'][1], value) - num_types = (int, float) - if not (_is_comparable(left) and - _is_comparable(right)): + if not ((_is_actual_number(left) and + _is_actual_number(right)) or + (isinstance(left, string_type) and + isinstance(right, string_type))): return None return comparator_func(left, right)