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 .root_path (), * args ):
17
+ def arrappend (
18
+ self , name : str , path : Optional [str ] = Path .root_path (), * 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.root_path(), *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,73 @@ 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 .root_path ()):
64
+ def arrlen (
65
+ self , name : str , path : Optional [str ] = Path .root_path ()
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 .root_path (), index = - 1 ):
74
+ def arrpop (
75
+ self ,
76
+ name : str ,
77
+ path : Optional [str ] = Path .root_path (),
78
+ index : Optional [int ] = - 1 ,
79
+ ) -> List [Union [str , None ]]:
80
+
60
81
"""Pop the element at ``index`` in the array JSON value under
61
82
``path`` at key ``name``.
62
83
63
84
For more information: https://oss.redis.com/redisjson/commands/#jsonarrpop
64
85
""" # noqa
65
86
return self .execute_command ("JSON.ARRPOP" , name , str (path ), index )
66
87
67
- def arrtrim (self , name , path , start , stop ):
88
+ def arrtrim (
89
+ self , name : str , path : str , start : int , stop : int
90
+ ) -> List [Union [int , None ]]:
68
91
"""Trim the array JSON value under ``path`` at key ``name`` to the
69
92
inclusive range given by ``start`` and ``stop``.
70
93
71
94
For more information: https://oss.redis.com/redisjson/commands/#jsonarrtrim
72
95
""" # noqa
73
96
return self .execute_command ("JSON.ARRTRIM" , name , str (path ), start , stop )
74
97
75
- def type (self , name , path = Path .root_path ()):
98
+ def type (self , name : str , path : Optional [ str ] = Path .root_path ()) -> List [ str ] :
76
99
"""Get the type of the JSON value under ``path`` from key ``name``.
77
100
78
101
For more information: https://oss.redis.com/redisjson/commands/#jsontype
79
102
""" # noqa
80
103
return self .execute_command ("JSON.TYPE" , name , str (path ))
81
104
82
- def resp (self , name , path = Path .root_path ()):
105
+ def resp (self , name : str , path : Optional [ str ] = Path .root_path ()) -> List :
83
106
"""Return the JSON value under ``path`` at key ``name``.
84
107
85
108
For more information: https://oss.redis.com/redisjson/commands/#jsonresp
86
109
""" # noqa
87
110
return self .execute_command ("JSON.RESP" , name , str (path ))
88
111
89
- def objkeys (self , name , path = Path .root_path ()):
112
+ def objkeys (
113
+ self , name : str , path : Optional [str ] = Path .root_path ()
114
+ ) -> List [Union [List [str ], None ]]:
90
115
"""Return the key names in the dictionary JSON value under ``path`` at
91
116
key ``name``.
92
117
93
118
For more information: https://oss.redis.com/redisjson/commands/#jsonobjkeys
94
119
""" # noqa
95
120
return self .execute_command ("JSON.OBJKEYS" , name , str (path ))
96
121
97
- def objlen (self , name , path = Path .root_path ()):
122
+ def objlen (self , name : str , path : Optional [ str ] = Path .root_path ()) -> int :
98
123
"""Return the length of the dictionary JSON value under ``path`` at key
99
124
``name``.
100
125
101
126
For more information: https://oss.redis.com/redisjson/commands/#jsonobjlen
102
127
""" # noqa
103
128
return self .execute_command ("JSON.OBJLEN" , name , str (path ))
104
129
105
- def numincrby (self , name , path , number ) :
130
+ def numincrby (self , name : str , path : str , number : int ) -> str :
106
131
"""Increment the numeric (integer or floating point) JSON value under
107
132
``path`` at key ``name`` by the provided ``number``.
108
133
@@ -113,7 +138,7 @@ def numincrby(self, name, path, number):
113
138
)
114
139
115
140
@deprecated (version = "4.0.0" , reason = "deprecated since redisjson 1.0.0" )
116
- def nummultby (self , name , path , number ) :
141
+ def nummultby (self , name : str , path : str , number : int ) -> str :
117
142
"""Multiply the numeric (integer or floating point) JSON value under
118
143
``path`` at key ``name`` with the provided ``number``.
119
144
@@ -123,7 +148,7 @@ def nummultby(self, name, path, number):
123
148
"JSON.NUMMULTBY" , name , str (path ), self ._encode (number )
124
149
)
125
150
126
- def clear (self , name , path = Path .root_path ()):
151
+ def clear (self , name : str , path : Optional [ str ] = Path .root_path ()) -> int :
127
152
"""
128
153
Empty arrays and objects (to have zero slots/keys without deleting the
129
154
array/object).
@@ -135,7 +160,7 @@ def clear(self, name, path=Path.root_path()):
135
160
""" # noqa
136
161
return self .execute_command ("JSON.CLEAR" , name , str (path ))
137
162
138
- def delete (self , key , path = Path .root_path ()):
163
+ def delete (self , key : str , path : Optional [ str ] = Path .root_path ()) -> int :
139
164
"""Delete the JSON value stored at key ``key`` under ``path``.
140
165
141
166
For more information: https://oss.redis.com/redisjson/commands/#jsondel
@@ -145,7 +170,9 @@ def delete(self, key, path=Path.root_path()):
145
170
# forget is an alias for delete
146
171
forget = delete
147
172
148
- def get (self , name , * args , no_escape = False ):
173
+ def get (
174
+ self , name : str , * args , no_escape : Optional [bool ] = False
175
+ ) -> List [JsonType ]:
149
176
"""
150
177
Get the object stored as a JSON value at key ``name``.
151
178
@@ -173,7 +200,7 @@ def get(self, name, *args, no_escape=False):
173
200
except TypeError :
174
201
return None
175
202
176
- def mget (self , keys , path ) :
203
+ def mget (self , keys : List [ str ] , path : str ) -> List [ JsonType ] :
177
204
"""
178
205
Get the objects stored as a JSON values under ``path``. ``keys``
179
206
is a list of one or more keys.
@@ -185,7 +212,15 @@ def mget(self, keys, path):
185
212
pieces .append (str (path ))
186
213
return self .execute_command ("JSON.MGET" , * pieces )
187
214
188
- def set (self , name , path , obj , nx = False , xx = False , decode_keys = False ):
215
+ def set (
216
+ self ,
217
+ name : str ,
218
+ path : str ,
219
+ obj : JsonType ,
220
+ nx : Optional [bool ] = False ,
221
+ xx : Optional [bool ] = False ,
222
+ decode_keys : Optional [bool ] = False ,
223
+ ) -> Optional [str ]:
189
224
"""
190
225
Set the JSON value at key ``name`` under the ``path`` to ``obj``.
191
226
@@ -216,7 +251,15 @@ def set(self, name, path, obj, nx=False, xx=False, decode_keys=False):
216
251
pieces .append ("XX" )
217
252
return self .execute_command ("JSON.SET" , * pieces )
218
253
219
- def set_file (self , name , path , file_name , nx = False , xx = False , decode_keys = False ):
254
+ def set_file (
255
+ self ,
256
+ name : str ,
257
+ path : str ,
258
+ file_name : str ,
259
+ nx : Optional [bool ] = False ,
260
+ xx : Optional [bool ] = False ,
261
+ decode_keys : Optional [bool ] = False ,
262
+ ) -> Optional [str ]:
220
263
"""
221
264
Set the JSON value at key ``name`` under the ``path`` to the content
222
265
of the json file ``file_name``.
@@ -233,7 +276,14 @@ def set_file(self, name, path, file_name, nx=False, xx=False, decode_keys=False)
233
276
234
277
return self .set (name , path , file_content , nx = nx , xx = xx , decode_keys = decode_keys )
235
278
236
- def set_path (self , json_path , root_folder , nx = False , xx = False , decode_keys = False ):
279
+ def set_path (
280
+ self ,
281
+ json_path : str ,
282
+ root_folder : str ,
283
+ nx : Optional [bool ] = False ,
284
+ xx : Optional [bool ] = False ,
285
+ decode_keys : Optional [bool ] = False ,
286
+ ) -> List [Dict [str , bool ]]:
237
287
"""
238
288
Iterate over ``root_folder`` and set each JSON file to a value
239
289
under ``json_path`` with the file name as the key.
@@ -264,7 +314,7 @@ def set_path(self, json_path, root_folder, nx=False, xx=False, decode_keys=False
264
314
265
315
return set_files_result
266
316
267
- def strlen (self , name , path = None ):
317
+ def strlen (self , name : str , path : Optional [ str ] = None ) -> List [ Union [ int , None ]] :
268
318
"""Return the length of the string JSON value under ``path`` at key
269
319
``name``.
270
320
@@ -275,15 +325,19 @@ def strlen(self, name, path=None):
275
325
pieces .append (str (path ))
276
326
return self .execute_command ("JSON.STRLEN" , * pieces )
277
327
278
- def toggle (self , name , path = Path .root_path ()):
328
+ def toggle (
329
+ self , name : str , path : Optional [str ] = Path .root_path ()
330
+ ) -> Union [bool , List [Optional [int ]]]:
279
331
"""Toggle boolean value under ``path`` at key ``name``.
280
332
returning the new value.
281
333
282
334
For more information: https://oss.redis.com/redisjson/commands/#jsontoggle
283
335
""" # noqa
284
336
return self .execute_command ("JSON.TOGGLE" , name , str (path ))
285
337
286
- def strappend (self , name , value , path = Path .root_path ()):
338
+ def strappend (
339
+ self , name : str , value : str , path : Optional [int ] = Path .root_path ()
340
+ ) -> Union [int , List [Optional [int ]]]:
287
341
"""Append to the string JSON value. If two options are specified after
288
342
the key name, the path is determined to be the first. If a single
289
343
option is passed, then the root_path (i.e Path.root_path()) is used.
@@ -293,11 +347,16 @@ def strappend(self, name, value, path=Path.root_path()):
293
347
pieces = [name , str (path ), self ._encode (value )]
294
348
return self .execute_command ("JSON.STRAPPEND" , * pieces )
295
349
296
- def debug (self , subcommand , key = None , path = Path .root_path ()):
350
+ def debug (
351
+ self ,
352
+ subcommand : str ,
353
+ key : Optional [str ] = None ,
354
+ path : Optional [str ] = Path .root_path (),
355
+ ) -> Union [int , List [str ]]:
297
356
"""Return the memory usage in bytes of a value under ``path`` from
298
357
key ``name``.
299
358
300
- For more information: https://oss.redis.com/redisjson/commands/#jsondebg
359
+ For more information: https://oss.redis.com/redisjson/commands/#jsondebug
301
360
""" # noqa
302
361
valid_subcommands = ["MEMORY" , "HELP" ]
303
362
if subcommand not in valid_subcommands :
0 commit comments