Skip to content

Commit 2472f37

Browse files
committed
Define type of JSON-like data
1 parent 36e072b commit 2472f37

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

jsonpath_rfc9535/environment.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@
3737
from .tokens import Token
3838

3939

40+
JSONLikeData = Union[
41+
List[Any],
42+
Dict[str, Any],
43+
str,
44+
int,
45+
float,
46+
None,
47+
bool,
48+
]
49+
"""JSON-like data, as you would get from `json.load()`."""
50+
51+
4052
class JSONPathEnvironment:
4153
"""JSONPath configuration.
4254
@@ -89,7 +101,7 @@ def compile(self, path: str) -> JSONPath: # noqa: A003
89101
def finditer(
90102
self,
91103
path: str,
92-
data: Union[List[Any], Dict[str, Any], str],
104+
data: JSONLikeData,
93105
) -> Iterable[JSONPathNode]:
94106
"""Generate `JSONPathNode` instances for each match of _path_ in _data_.
95107
@@ -110,7 +122,7 @@ def finditer(
110122
def findall(
111123
self,
112124
path: str,
113-
data: Union[List[Any], Dict[str, Any], str],
125+
data: JSONLikeData,
114126
) -> List[object]:
115127
"""Find all values in _data_ matching JSONPath query _path_.
116128
@@ -131,7 +143,7 @@ def findall(
131143
def query(
132144
self,
133145
path: str,
134-
data: Union[List[Any], Dict[str, Any], str],
146+
data: JSONLikeData,
135147
) -> JSONPathNodeList:
136148
"""Apply the JSONPath query _path_ to JSON-like _data_.
137149

jsonpath_rfc9535/expressions.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
from abc import ABC
77
from abc import abstractmethod
88
from typing import TYPE_CHECKING
9-
from typing import Any
10-
from typing import Dict
119
from typing import Generic
1210
from typing import List
1311
from typing import Sequence
1412
from typing import TypeVar
15-
from typing import Union
1613

1714
from jsonpath_rfc9535.function_extensions.filter_function import ExpressionType
1815
from jsonpath_rfc9535.function_extensions.filter_function import FilterFunction
@@ -21,6 +18,7 @@
2118
from .node import JSONPathNodeList
2219

2320
if TYPE_CHECKING:
21+
from .environment import JSONLikeData
2422
from .environment import JSONPathEnvironment
2523
from .path import JSONPath
2624
from .tokens import Token
@@ -349,7 +347,7 @@ def __init__(
349347
*,
350348
env: JSONPathEnvironment,
351349
current: object,
352-
root: Union[List[Any], Dict[str, Any], str],
350+
root: JSONLikeData,
353351
) -> None:
354352
self.env = env
355353
self.current = current

jsonpath_rfc9535/node.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""JSONPath node and node list definitions."""
22

3-
from typing import Any
4-
from typing import Dict
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
56
from typing import List
67
from typing import Tuple
78
from typing import Union
89

10+
if TYPE_CHECKING:
11+
from .environment import JSONLikeData
12+
913

1014
class JSONPathNode:
1115
"""A JSON-like value and its location in a JSON document.
@@ -26,11 +30,11 @@ def __init__(
2630
*,
2731
value: object,
2832
parts: Tuple[Union[int, str], ...],
29-
root: Union[List[Any], Dict[str, Any], str],
33+
root: JSONLikeData,
3034
) -> None:
3135
self.value: object = value
3236
self.parts: Tuple[Union[int, str], ...] = parts
33-
self.root: Union[List[Any], Dict[str, Any], str] = root
37+
self.root = root
3438

3539
def path(self) -> str:
3640
"""Return the normalized path to this node."""
@@ -53,14 +57,14 @@ def values(self) -> List[object]:
5357
return [node.value for node in self]
5458

5559
def values_or_singular(self) -> object:
56-
"""Return the values from this node list."""
60+
"""Return values from this node list, or a single value if there's one node."""
5761
if len(self) == 1:
5862
return self[0].value
5963
return [node.value for node in self]
6064

6165
def empty(self) -> bool:
6266
"""Return `True` if this node list is empty."""
63-
return not bool(self)
67+
return not self
6468

6569
def __str__(self) -> str:
6670
return f"NodeList{super().__str__()}"

jsonpath_rfc9535/path.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
from __future__ import annotations
44

55
from typing import TYPE_CHECKING
6-
from typing import Any
7-
from typing import Dict
86
from typing import Iterable
97
from typing import List
108
from typing import Tuple
11-
from typing import Union
129

1310
from .node import JSONPathNode
1411
from .node import JSONPathNodeList
@@ -17,6 +14,7 @@
1714
from .selectors import PropertySelector
1815

1916
if TYPE_CHECKING:
17+
from .environment import JSONLikeData
2018
from .environment import JSONPathEnvironment
2119
from .segments import JSONPathSegment
2220

@@ -53,7 +51,7 @@ def __hash__(self) -> int:
5351

5452
def finditer(
5553
self,
56-
data: Union[List[Any], Dict[str, Any], str],
54+
data: JSONLikeData,
5755
) -> Iterable[JSONPathNode]:
5856
"""Generate `JSONPathNode` instances for each match of this path in _data_.
5957
@@ -83,7 +81,7 @@ def finditer(
8381

8482
def findall(
8583
self,
86-
data: Union[List[Any], Dict[str, Any], str],
84+
data: JSONLikeData,
8785
) -> List[object]:
8886
"""Find all values in _data_ matching this JSONPath.
8987
@@ -102,7 +100,7 @@ def findall(
102100

103101
def query(
104102
self,
105-
data: Union[List[Any], Dict[str, Any], str],
103+
data: JSONLikeData,
106104
) -> JSONPathNodeList:
107105
"""Apply this JSONPath query to JSON-like _data_ and return a node list.
108106

0 commit comments

Comments
 (0)