From 3d33869f554691ad1c4d614aad0c626f5e034721 Mon Sep 17 00:00:00 2001 From: Denis Guerra Date: Tue, 7 Jan 2025 22:36:05 -0300 Subject: [PATCH] Fix bool filter type to handle None values --- jsonpath_ng/ext/filter.py | 7 +------ tests/test_jsonpath_rw_ext.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/jsonpath_ng/ext/filter.py b/jsonpath_ng/ext/filter.py index e0bd48a..6ec7d1c 100644 --- a/jsonpath_ng/ext/filter.py +++ b/jsonpath_ng/ext/filter.py @@ -104,16 +104,11 @@ def find(self, datum): found = [] for data in datum: value = data.value - if isinstance(self.value, int): + if type(self.value) is int: try: value = int(value) except ValueError: continue - elif isinstance(self.value, bool): - try: - value = bool(value) - except ValueError: - continue if OPERATOR_MAP[self.op](value, self.value): found.append(data) diff --git a/tests/test_jsonpath_rw_ext.py b/tests/test_jsonpath_rw_ext.py index a14fd72..ca1c3e9 100644 --- a/tests/test_jsonpath_rw_ext.py +++ b/tests/test_jsonpath_rw_ext.py @@ -484,6 +484,19 @@ ["green"], id="boolean-filter-string-true-string-literal", ), + pytest.param( + "foo[?flag = true].color", + { + "foo": [ + {"color": "blue", "flag": True}, + {"color": "green", "flag": 2}, + {"color": "red", "flag": "hi"}, + {"color": "gray", "flag": None}, + ] + }, + ["blue"], + id="boolean-filter-with-null", + ), )