From a06c8b7f573db1b17fc0274ccf399128344a03aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=8E=AE=20=28Jade=20Lin=29?= Date: Wed, 16 Sep 2020 00:36:38 +0800 Subject: [PATCH 1/3] Fix:IndexError raised from method Array.find --- jsonpath/core.py | 19 +++++++++---------- tests/test_lark.py | 4 ++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/jsonpath/core.py b/jsonpath/core.py index 6d2ef92..73f2ce4 100644 --- a/jsonpath/core.py +++ b/jsonpath/core.py @@ -9,6 +9,7 @@ import weakref from abc import abstractmethod +from contextlib import suppress from contextvars import ContextVar from typing import ( Any, @@ -455,16 +456,14 @@ def _get_partial_expression(self) -> str: return f"[{self.idx!s}]" def find(self, element: Any) -> List[Any]: - if self.idx is None and isinstance(element, list): - return element - elif ( - isinstance(self.idx, int) - and isinstance(element, list) - and self.idx < len(element) - ): - return [element[self.idx]] - elif isinstance(self.idx, Slice): - return self.idx.find(element) + if isinstance(element, list): + if self.idx is None: + return element + elif isinstance(self.idx, int): + with suppress(IndexError): + return [element[self.idx]] + elif isinstance(self.idx, Slice): + return self.idx.find(element) raise JSONPathFindError diff --git a/tests/test_lark.py b/tests/test_lark.py index 463401a..92ff6e9 100644 --- a/tests/test_lark.py +++ b/tests/test_lark.py @@ -132,6 +132,10 @@ def test_parse_check_and_extract(expression, data, expect): ("boo.*.boo", {"boo": {"boo": {"boo": 1}, "bar": {"bar": 2}}}, [1]), ("boo.*.boo", {"boo": {"boo": {"boo": 1}, "bar": 1}}, [1]), ("$[0]", [1, 2], [1]), + ("$[1]", [1, 2], [2]), + ("$[2]", [1, 2], []), + ("$[-1]", [], []), + ("$[-1]", [1], [1]), ("boo[0]", {"boo": [1, 2]}, [1]), ("$[*]", [1, 2], [1, 2]), ("$[:1]", [1, 2], [1]), From 34a90a4caf190124fc5b7ccfc9ca8600510ffb2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=8E=AE=20=28Jade=20Lin=29?= Date: Wed, 16 Sep 2020 00:58:08 +0800 Subject: [PATCH 2/3] New:Raise AssertionError if Array.idx is not valid --- jsonpath/core.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jsonpath/core.py b/jsonpath/core.py index 73f2ce4..9712705 100644 --- a/jsonpath/core.py +++ b/jsonpath/core.py @@ -464,6 +464,8 @@ def find(self, element: Any) -> List[Any]: return [element[self.idx]] elif isinstance(self.idx, Slice): return self.idx.find(element) + else: + raise AssertionError(f"{self.idx=} is not valid") raise JSONPathFindError From 586088aa4778f0fd0f7d7b91c41d3ee04e29c98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=8E=AE=20=28Jade=20Lin=29?= Date: Wed, 16 Sep 2020 01:05:18 +0800 Subject: [PATCH 3/3] Fix:f-strings debugging was implemented in Python 3.8 not 3.7 [f-strings support = for self-documenting expressions and debugging](https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging) --- jsonpath/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonpath/core.py b/jsonpath/core.py index 9712705..6cbf7aa 100644 --- a/jsonpath/core.py +++ b/jsonpath/core.py @@ -465,7 +465,7 @@ def find(self, element: Any) -> List[Any]: elif isinstance(self.idx, Slice): return self.idx.find(element) else: - raise AssertionError(f"{self.idx=} is not valid") + raise AssertionError(f"self.idx={self.idx!r} is not valid") raise JSONPathFindError