1
1
import os
2
2
from json import JSONDecodeError , loads
3
+ from typing import Dict , List , Optional , Union
3
4
4
5
from deprecated import deprecated
5
6
6
7
from redis .exceptions import DataError
7
8
9
+ from ._util import JsonType
8
10
from .decoders import decode_dict_keys
9
11
from .path import Path
10
12
11
13
12
14
class JSONCommands :
13
15
"""json commands."""
14
16
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 ]]:
16
20
"""Append the objects ``args`` to the array under the
17
21
``path` in key ``name``.
18
22
@@ -23,7 +27,14 @@ def arrappend(self, name, path=Path.rootPath(), *args):
23
27
pieces .append (self ._encode (o ))
24
28
return self .execute_command ("JSON.ARRAPPEND" , * pieces )
25
29
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 ]]:
27
38
"""
28
39
Return the index of ``scalar`` in the JSON array under ``path`` at key
29
40
``name``.
@@ -37,7 +48,9 @@ def arrindex(self, name, path, scalar, start=0, stop=-1):
37
48
"JSON.ARRINDEX" , name , str (path ), self ._encode (scalar ), start , stop
38
49
)
39
50
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 ]]:
41
54
"""Insert the objects ``args`` to the array at index ``index``
42
55
under the ``path` in key ``name``.
43
56
@@ -48,61 +61,72 @@ def arrinsert(self, name, path, index, *args):
48
61
pieces .append (self ._encode (o ))
49
62
return self .execute_command ("JSON.ARRINSERT" , * pieces )
50
63
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 ]]:
52
67
"""Return the length of the array JSON value under ``path``
53
68
at key``name``.
54
69
55
70
For more information: https://oss.redis.com/redisjson/commands/#jsonarrlen
56
71
""" # noqa
57
72
return self .execute_command ("JSON.ARRLEN" , name , str (path ))
58
73
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 ]]:
60
80
"""Pop the element at ``index`` in the array JSON value under
61
81
``path`` at key ``name``.
62
82
63
83
For more information: https://oss.redis.com/redisjson/commands/#jsonarrpop
64
84
""" # noqa
65
85
return self .execute_command ("JSON.ARRPOP" , name , str (path ), index )
66
86
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 ]]:
68
90
"""Trim the array JSON value under ``path`` at key ``name`` to the
69
91
inclusive range given by ``start`` and ``stop``.
70
92
71
93
For more information: https://oss.redis.com/redisjson/commands/#jsonarrtrim
72
94
""" # noqa
73
95
return self .execute_command ("JSON.ARRTRIM" , name , str (path ), start , stop )
74
96
75
- def type (self , name , path = Path .rootPath ()):
97
+ def type (self , name : str , path : Optional [ str ] = Path .rootPath ()) -> List [ str ] :
76
98
"""Get the type of the JSON value under ``path`` from key ``name``.
77
99
78
100
For more information: https://oss.redis.com/redisjson/commands/#jsontype
79
101
""" # noqa
80
102
return self .execute_command ("JSON.TYPE" , name , str (path ))
81
103
82
- def resp (self , name , path = Path .rootPath ()):
104
+ def resp (self , name : str , path : Optional [ str ] = Path .rootPath ()) -> List :
83
105
"""Return the JSON value under ``path`` at key ``name``.
84
106
85
107
For more information: https://oss.redis.com/redisjson/commands/#jsonresp
86
108
""" # noqa
87
109
return self .execute_command ("JSON.RESP" , name , str (path ))
88
110
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 ]]:
90
114
"""Return the key names in the dictionary JSON value under ``path`` at
91
115
key ``name``.
92
116
93
117
For more information: https://oss.redis.com/redisjson/commands/#jsonobjkeys
94
118
""" # noqa
95
119
return self .execute_command ("JSON.OBJKEYS" , name , str (path ))
96
120
97
- def objlen (self , name , path = Path .rootPath ()):
121
+ def objlen (self , name : str , path : Optional [ str ] = Path .rootPath ()) -> int :
98
122
"""Return the length of the dictionary JSON value under ``path`` at key
99
123
``name``.
100
124
101
125
For more information: https://oss.redis.com/redisjson/commands/#jsonobjlen
102
126
""" # noqa
103
127
return self .execute_command ("JSON.OBJLEN" , name , str (path ))
104
128
105
- def numincrby (self , name , path , number ) :
129
+ def numincrby (self , name : str , path : str , number : int ) -> str :
106
130
"""Increment the numeric (integer or floating point) JSON value under
107
131
``path`` at key ``name`` by the provided ``number``.
108
132
@@ -113,7 +137,7 @@ def numincrby(self, name, path, number):
113
137
)
114
138
115
139
@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 :
117
141
"""Multiply the numeric (integer or floating point) JSON value under
118
142
``path`` at key ``name`` with the provided ``number``.
119
143
@@ -123,7 +147,7 @@ def nummultby(self, name, path, number):
123
147
"JSON.NUMMULTBY" , name , str (path ), self ._encode (number )
124
148
)
125
149
126
- def clear (self , name , path = Path .rootPath ()):
150
+ def clear (self , name : str , path : Optional [ str ] = Path .rootPath ()) -> int :
127
151
"""
128
152
Empty arrays and objects (to have zero slots/keys without deleting the
129
153
array/object).
@@ -135,7 +159,7 @@ def clear(self, name, path=Path.rootPath()):
135
159
""" # noqa
136
160
return self .execute_command ("JSON.CLEAR" , name , str (path ))
137
161
138
- def delete (self , key , path = Path .rootPath ()):
162
+ def delete (self , key : str , path : Optional [ str ] = Path .rootPath ()) -> int :
139
163
"""Delete the JSON value stored at key ``key`` under ``path``.
140
164
141
165
For more information: https://oss.redis.com/redisjson/commands/#jsondel
@@ -145,7 +169,9 @@ def delete(self, key, path=Path.rootPath()):
145
169
# forget is an alias for delete
146
170
forget = delete
147
171
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 ]:
149
175
"""
150
176
Get the object stored as a JSON value at key ``name``.
151
177
@@ -173,7 +199,7 @@ def get(self, name, *args, no_escape=False):
173
199
except TypeError :
174
200
return None
175
201
176
- def mget (self , keys , path ) :
202
+ def mget (self , keys : List [ str ] , path : str ) -> List [ JsonType ] :
177
203
"""
178
204
Get the objects stored as a JSON values under ``path``. ``keys``
179
205
is a list of one or more keys.
@@ -185,7 +211,15 @@ def mget(self, keys, path):
185
211
pieces .append (str (path ))
186
212
return self .execute_command ("JSON.MGET" , * pieces )
187
213
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 ]:
189
223
"""
190
224
Set the JSON value at key ``name`` under the ``path`` to ``obj``.
191
225
@@ -216,7 +250,15 @@ def set(self, name, path, obj, nx=False, xx=False, decode_keys=False):
216
250
pieces .append ("XX" )
217
251
return self .execute_command ("JSON.SET" , * pieces )
218
252
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 ]:
220
262
"""
221
263
Set the JSON value at key ``name`` under the ``path`` to the content
222
264
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)
233
275
234
276
return self .set (name , path , file_content , nx = nx , xx = xx , decode_keys = decode_keys )
235
277
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 ]]:
237
286
"""
238
287
Iterate over ``root_folder`` and set each JSON file to a value
239
288
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
264
313
265
314
return set_files_result
266
315
267
- def strlen (self , name , path = None ):
316
+ def strlen (self , name : str , path : Optional [ str ] = None ) -> List [ Union [ int , None ]] :
268
317
"""Return the length of the string JSON value under ``path`` at key
269
318
``name``.
270
319
@@ -275,15 +324,19 @@ def strlen(self, name, path=None):
275
324
pieces .append (str (path ))
276
325
return self .execute_command ("JSON.STRLEN" , * pieces )
277
326
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 ]]]:
279
330
"""Toggle boolean value under ``path`` at key ``name``.
280
331
returning the new value.
281
332
282
333
For more information: https://oss.redis.com/redisjson/commands/#jsontoggle
283
334
""" # noqa
284
335
return self .execute_command ("JSON.TOGGLE" , name , str (path ))
285
336
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 ]]]:
287
340
"""Append to the string JSON value. If two options are specified after
288
341
the key name, the path is determined to be the first. If a single
289
342
option is passed, then the rootpath (i.e Path.rootPath()) is used.
@@ -293,11 +346,16 @@ def strappend(self, name, value, path=Path.rootPath()):
293
346
pieces = [name , str (path ), self ._encode (value )]
294
347
return self .execute_command ("JSON.STRAPPEND" , * pieces )
295
348
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 ]]:
297
355
"""Return the memory usage in bytes of a value under ``path`` from
298
356
key ``name``.
299
357
300
- For more information: https://oss.redis.com/redisjson/commands/#jsondebg
358
+ For more information: https://oss.redis.com/redisjson/commands/#jsondebug
301
359
""" # noqa
302
360
valid_subcommands = ["MEMORY" , "HELP" ]
303
361
if subcommand not in valid_subcommands :
0 commit comments