Skip to content

Comments

Fix TypeError on ordering comparison between string and number#347

Open
bysiber wants to merge 1 commit intojmespath:developfrom
bysiber:fix/comparator-mixed-types
Open

Fix TypeError on ordering comparison between string and number#347
bysiber wants to merge 1 commit intojmespath:developfrom
bysiber:fix/comparator-mixed-types

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

Ordering comparators (<, <=, >, >=) crash with TypeError when comparing a string to a number:

>>> jmespath.search('[?a < b]', [{"a": "foo", "b": 5}])
TypeError: '<' not supported between instances of 'str' and 'int'

Per the JMESPath spec, ordering comparisons on incompatible types should return null.

The bug is in _is_comparable() — it returns True for numbers or strings independently. When one operand is a string and the other is a number, both pass the individual check, but operator.lt("foo", 5) raises TypeError in Python 3.

The fix requires 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 before string comparisons were added.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant