Skip to content

Commit d4f09d4

Browse files
committed
add type hints for json commands
1 parent 8499adc commit d4f09d4

File tree

2 files changed

+85
-24
lines changed

2 files changed

+85
-24
lines changed

redis/commands/json/_util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from typing import Any, Dict, List, Union
2+
3+
JsonType = Union[str, int, float, bool, None, Dict[str, Any], List[Any]]

redis/commands/json/commands.py

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import os
22
from json import JSONDecodeError, loads
3+
from typing import Dict, List, Optional, Union
34

45
from deprecated import deprecated
56

67
from redis.exceptions import DataError
78

9+
from ._util import JsonType
810
from .decoders import decode_dict_keys
911
from .path import Path
1012

1113

1214
class JSONCommands:
1315
"""json commands."""
1416

15-
def arrappend(self, name, path=Path.rootPath(), *args):
17+
def arrappend(
18+
self, name: str, path: Optional[str] = Path.rootPath(), *args: List[JsonType]
19+
) -> List[Union[int, None]]:
1620
"""Append the objects ``args`` to the array under the
1721
``path` in key ``name``.
1822
@@ -23,7 +27,14 @@ def arrappend(self, name, path=Path.rootPath(), *args):
2327
pieces.append(self._encode(o))
2428
return self.execute_command("JSON.ARRAPPEND", *pieces)
2529

26-
def arrindex(self, name, path, scalar, start=0, stop=-1):
30+
def arrindex(
31+
self,
32+
name: str,
33+
path: str,
34+
scalar: int,
35+
start: Optional[int] = 0,
36+
stop: Optional[int] = -1,
37+
) -> List[Union[int, None]]:
2738
"""
2839
Return the index of ``scalar`` in the JSON array under ``path`` at key
2940
``name``.
@@ -37,7 +48,9 @@ def arrindex(self, name, path, scalar, start=0, stop=-1):
3748
"JSON.ARRINDEX", name, str(path), self._encode(scalar), start, stop
3849
)
3950

40-
def arrinsert(self, name, path, index, *args):
51+
def arrinsert(
52+
self, name: str, path: str, index: int, *args: List[JsonType]
53+
) -> List[Union[int, None]]:
4154
"""Insert the objects ``args`` to the array at index ``index``
4255
under the ``path` in key ``name``.
4356
@@ -48,61 +61,72 @@ def arrinsert(self, name, path, index, *args):
4861
pieces.append(self._encode(o))
4962
return self.execute_command("JSON.ARRINSERT", *pieces)
5063

51-
def arrlen(self, name, path=Path.rootPath()):
64+
def arrlen(
65+
self, name: str, path: Optional[str] = Path.rootPath()
66+
) -> List[Union[int, None]]:
5267
"""Return the length of the array JSON value under ``path``
5368
at key``name``.
5469
5570
For more information: https://oss.redis.com/redisjson/commands/#jsonarrlen
5671
""" # noqa
5772
return self.execute_command("JSON.ARRLEN", name, str(path))
5873

59-
def arrpop(self, name, path=Path.rootPath(), index=-1):
74+
def arrpop(
75+
self,
76+
name: str,
77+
path: Optional[str] = Path.rootPath(),
78+
index: Optional[int] = -1,
79+
) -> List[Union[str, None]]:
6080
"""Pop the element at ``index`` in the array JSON value under
6181
``path`` at key ``name``.
6282
6383
For more information: https://oss.redis.com/redisjson/commands/#jsonarrpop
6484
""" # noqa
6585
return self.execute_command("JSON.ARRPOP", name, str(path), index)
6686

67-
def arrtrim(self, name, path, start, stop):
87+
def arrtrim(
88+
self, name: str, path: str, start: int, stop: int
89+
) -> List[Union[int, None]]:
6890
"""Trim the array JSON value under ``path`` at key ``name`` to the
6991
inclusive range given by ``start`` and ``stop``.
7092
7193
For more information: https://oss.redis.com/redisjson/commands/#jsonarrtrim
7294
""" # noqa
7395
return self.execute_command("JSON.ARRTRIM", name, str(path), start, stop)
7496

75-
def type(self, name, path=Path.rootPath()):
97+
def type(self, name: str, path: Optional[str] = Path.rootPath()) -> List[str]:
7698
"""Get the type of the JSON value under ``path`` from key ``name``.
7799
78100
For more information: https://oss.redis.com/redisjson/commands/#jsontype
79101
""" # noqa
80102
return self.execute_command("JSON.TYPE", name, str(path))
81103

82-
def resp(self, name, path=Path.rootPath()):
104+
def resp(self, name: str, path: Optional[str] = Path.rootPath()) -> List:
83105
"""Return the JSON value under ``path`` at key ``name``.
84106
85107
For more information: https://oss.redis.com/redisjson/commands/#jsonresp
86108
""" # noqa
87109
return self.execute_command("JSON.RESP", name, str(path))
88110

89-
def objkeys(self, name, path=Path.rootPath()):
111+
def objkeys(
112+
self, name: str, path: Optional[str] = Path.rootPath()
113+
) -> List[Union[List[str], None]]:
90114
"""Return the key names in the dictionary JSON value under ``path`` at
91115
key ``name``.
92116
93117
For more information: https://oss.redis.com/redisjson/commands/#jsonobjkeys
94118
""" # noqa
95119
return self.execute_command("JSON.OBJKEYS", name, str(path))
96120

97-
def objlen(self, name, path=Path.rootPath()):
121+
def objlen(self, name: str, path: Optional[str] = Path.rootPath()) -> int:
98122
"""Return the length of the dictionary JSON value under ``path`` at key
99123
``name``.
100124
101125
For more information: https://oss.redis.com/redisjson/commands/#jsonobjlen
102126
""" # noqa
103127
return self.execute_command("JSON.OBJLEN", name, str(path))
104128

105-
def numincrby(self, name, path, number):
129+
def numincrby(self, name: str, path: str, number: int) -> str:
106130
"""Increment the numeric (integer or floating point) JSON value under
107131
``path`` at key ``name`` by the provided ``number``.
108132
@@ -113,7 +137,7 @@ def numincrby(self, name, path, number):
113137
)
114138

115139
@deprecated(version="4.0.0", reason="deprecated since redisjson 1.0.0")
116-
def nummultby(self, name, path, number):
140+
def nummultby(self, name: str, path: str, number: int) -> str:
117141
"""Multiply the numeric (integer or floating point) JSON value under
118142
``path`` at key ``name`` with the provided ``number``.
119143
@@ -123,7 +147,7 @@ def nummultby(self, name, path, number):
123147
"JSON.NUMMULTBY", name, str(path), self._encode(number)
124148
)
125149

126-
def clear(self, name, path=Path.rootPath()):
150+
def clear(self, name: str, path: Optional[str] = Path.rootPath()) -> int:
127151
"""
128152
Empty arrays and objects (to have zero slots/keys without deleting the
129153
array/object).
@@ -135,7 +159,7 @@ def clear(self, name, path=Path.rootPath()):
135159
""" # noqa
136160
return self.execute_command("JSON.CLEAR", name, str(path))
137161

138-
def delete(self, key, path=Path.rootPath()):
162+
def delete(self, key: str, path: Optional[str] = Path.rootPath()) -> int:
139163
"""Delete the JSON value stored at key ``key`` under ``path``.
140164
141165
For more information: https://oss.redis.com/redisjson/commands/#jsondel
@@ -145,7 +169,9 @@ def delete(self, key, path=Path.rootPath()):
145169
# forget is an alias for delete
146170
forget = delete
147171

148-
def get(self, name, *args, no_escape=False):
172+
def get(
173+
self, name: str, *args, no_escape: Optional[bool] = False
174+
) -> List[JsonType]:
149175
"""
150176
Get the object stored as a JSON value at key ``name``.
151177
@@ -173,7 +199,7 @@ def get(self, name, *args, no_escape=False):
173199
except TypeError:
174200
return None
175201

176-
def mget(self, keys, path):
202+
def mget(self, keys: List[str], path: str) -> List[JsonType]:
177203
"""
178204
Get the objects stored as a JSON values under ``path``. ``keys``
179205
is a list of one or more keys.
@@ -185,7 +211,15 @@ def mget(self, keys, path):
185211
pieces.append(str(path))
186212
return self.execute_command("JSON.MGET", *pieces)
187213

188-
def set(self, name, path, obj, nx=False, xx=False, decode_keys=False):
214+
def set(
215+
self,
216+
name: str,
217+
path: str,
218+
obj: JsonType,
219+
nx: Optional[bool] = False,
220+
xx: Optional[bool] = False,
221+
decode_keys: Optional[bool] = False,
222+
) -> Optional[str]:
189223
"""
190224
Set the JSON value at key ``name`` under the ``path`` to ``obj``.
191225
@@ -216,7 +250,15 @@ def set(self, name, path, obj, nx=False, xx=False, decode_keys=False):
216250
pieces.append("XX")
217251
return self.execute_command("JSON.SET", *pieces)
218252

219-
def set_file(self, name, path, file_name, nx=False, xx=False, decode_keys=False):
253+
def set_file(
254+
self,
255+
name: str,
256+
path: str,
257+
file_name: str,
258+
nx: Optional[bool] = False,
259+
xx: Optional[bool] = False,
260+
decode_keys: Optional[bool] = False,
261+
) -> Optional[str]:
220262
"""
221263
Set the JSON value at key ``name`` under the ``path`` to the content
222264
of the json file ``file_name``.
@@ -233,7 +275,14 @@ def set_file(self, name, path, file_name, nx=False, xx=False, decode_keys=False)
233275

234276
return self.set(name, path, file_content, nx=nx, xx=xx, decode_keys=decode_keys)
235277

236-
def set_path(self, json_path, root_folder, nx=False, xx=False, decode_keys=False):
278+
def set_path(
279+
self,
280+
json_path: str,
281+
root_folder: str,
282+
nx: Optional[bool] = False,
283+
xx: Optional[bool] = False,
284+
decode_keys: Optional[bool] = False,
285+
) -> List[Dict[str, bool]]:
237286
"""
238287
Iterate over ``root_folder`` and set each JSON file to a value
239288
under ``json_path`` with the file name as the key.
@@ -264,7 +313,7 @@ def set_path(self, json_path, root_folder, nx=False, xx=False, decode_keys=False
264313

265314
return set_files_result
266315

267-
def strlen(self, name, path=None):
316+
def strlen(self, name: str, path: Optional[str] = None) -> List[Union[int, None]]:
268317
"""Return the length of the string JSON value under ``path`` at key
269318
``name``.
270319
@@ -275,15 +324,19 @@ def strlen(self, name, path=None):
275324
pieces.append(str(path))
276325
return self.execute_command("JSON.STRLEN", *pieces)
277326

278-
def toggle(self, name, path=Path.rootPath()):
327+
def toggle(
328+
self, name: str, path: Optional[str] = Path.rootPath()
329+
) -> Union[bool, List[Optional[int]]]:
279330
"""Toggle boolean value under ``path`` at key ``name``.
280331
returning the new value.
281332
282333
For more information: https://oss.redis.com/redisjson/commands/#jsontoggle
283334
""" # noqa
284335
return self.execute_command("JSON.TOGGLE", name, str(path))
285336

286-
def strappend(self, name, value, path=Path.rootPath()):
337+
def strappend(
338+
self, name: str, value: str, path: Optional[int] = Path.rootPath()
339+
) -> Union[int, List[Optional[int]]]:
287340
"""Append to the string JSON value. If two options are specified after
288341
the key name, the path is determined to be the first. If a single
289342
option is passed, then the rootpath (i.e Path.rootPath()) is used.
@@ -293,11 +346,16 @@ def strappend(self, name, value, path=Path.rootPath()):
293346
pieces = [name, str(path), self._encode(value)]
294347
return self.execute_command("JSON.STRAPPEND", *pieces)
295348

296-
def debug(self, subcommand, key=None, path=Path.rootPath()):
349+
def debug(
350+
self,
351+
subcommand: str,
352+
key: Optional[str] = None,
353+
path: Optional[str] = Path.rootPath(),
354+
) -> Union[int, List[str]]:
297355
"""Return the memory usage in bytes of a value under ``path`` from
298356
key ``name``.
299357
300-
For more information: https://oss.redis.com/redisjson/commands/#jsondebg
358+
For more information: https://oss.redis.com/redisjson/commands/#jsondebug
301359
""" # noqa
302360
valid_subcommands = ["MEMORY", "HELP"]
303361
if subcommand not in valid_subcommands:

0 commit comments

Comments
 (0)