Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/redis/_parsers/response_callbacks.py: 33%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

39 statements  

1"""Response-callback dictionaries and the protocol/legacy selector. 

2 

3This module is the single source of truth for the mapping between Redis 

4command names and the Python-side callbacks that post-process raw parser 

5output into user-facing values. 

6 

7Six dictionaries are defined: 

8 

9* ``_RedisCallbacks`` — entries that produce the same Python value 

10 regardless of wire protocol or legacy-response selection. 

11* ``_RedisCallbacksRESP2`` — RESP2 wire, legacy Python shapes. 

12* ``_RedisCallbacksRESP3`` — RESP3 wire, Python shapes from the previous 

13 RESP3 callbacks. 

14* ``_RedisCallbacksRESP2Unified`` — RESP2 wire, unified Python shapes 

15 (``legacy_responses=False``). 

16* ``_RedisCallbacksRESP3Unified`` — RESP3 wire, unified Python shapes 

17 (``legacy_responses=False``). 

18* ``_RedisCallbacksRESP3toRESP2Legacy`` — RESP3 wire converted back to the 

19 legacy RESP2 Python shapes for users who keep ``legacy_responses=True`` 

20 on a RESP3 connection. 

21 

22``get_response_callbacks`` merges ``_RedisCallbacks`` with the appropriate 

23protocol-specific overlay. Callers wrap the returned dict in 

24``CaseInsensitiveDict``. 

25""" 

26 

27from typing import Any, Callable, Mapping, Optional 

28 

29from redis.himport import ( 

30 HIMPORT_DISCARD, 

31 HIMPORT_DISCARDALL, 

32 HIMPORT_PREPARE, 

33 HIMPORT_SET, 

34) 

35from redis.utils import str_if_bytes 

36 

37from .helpers import ( 

38 bool_ok, 

39 bzpop_score_resp3_to_resp2_legacy, 

40 bzpop_score_unified, 

41 float_or_none, 

42 hrandfield_resp3_to_resp2_legacy, 

43 hrandfield_unified, 

44 pairs_to_dict, 

45 parse_acl_getuser, 

46 parse_acl_getuser_resp3_to_resp2_legacy, 

47 parse_acl_getuser_unified, 

48 parse_acl_log, 

49 parse_acl_log_resp3_to_resp2_legacy, 

50 parse_acl_log_resp3_unified, 

51 parse_arinfo, 

52 parse_client_info, 

53 parse_client_kill, 

54 parse_client_list, 

55 parse_client_trackinginfo_resp3_to_resp2_legacy, 

56 parse_client_trackinginfo_unified, 

57 parse_cluster_info, 

58 parse_cluster_links_resp3_to_resp2_legacy, 

59 parse_cluster_links_unified, 

60 parse_cluster_nodes, 

61 parse_command, 

62 parse_command_resp3, 

63 parse_command_unified, 

64 parse_config_get, 

65 parse_config_get_resp3_to_resp2_legacy, 

66 parse_debug_object, 

67 parse_function_list_resp3_to_resp2_legacy, 

68 parse_function_list_unified, 

69 parse_function_stats_resp3_to_resp2_legacy, 

70 parse_function_stats_unified, 

71 parse_geopos_resp3_to_resp2_legacy, 

72 parse_geopos_unified, 

73 parse_geosearch_generic, 

74 parse_geosearch_generic_unified, 

75 parse_hscan, 

76 parse_increx_unified, 

77 parse_info, 

78 parse_lcs_idx_resp3_to_resp2_legacy, 

79 parse_lcs_idx_unified, 

80 parse_list_of_dicts, 

81 parse_list_of_dicts_resp3, 

82 parse_memory_stats, 

83 parse_memory_stats_resp3, 

84 parse_memory_stats_unified, 

85 parse_pubsub_numsub, 

86 parse_scan, 

87 parse_sentinel_get_master, 

88 parse_sentinel_master, 

89 parse_sentinel_master_resp3_to_resp2_legacy, 

90 parse_sentinel_master_unified, 

91 parse_sentinel_master_unified_resp3, 

92 parse_sentinel_masters, 

93 parse_sentinel_masters_resp3, 

94 parse_sentinel_masters_resp3_to_resp2_legacy, 

95 parse_sentinel_masters_unified, 

96 parse_sentinel_masters_unified_resp3, 

97 parse_sentinel_slaves_and_sentinels, 

98 parse_sentinel_slaves_and_sentinels_resp3, 

99 parse_sentinel_slaves_and_sentinels_resp3_to_resp2_legacy, 

100 parse_sentinel_slaves_and_sentinels_unified, 

101 parse_sentinel_slaves_and_sentinels_unified_resp3, 

102 parse_sentinel_state_resp3, 

103 parse_set_result, 

104 parse_slowlog_get, 

105 parse_stralgo, 

106 parse_stralgo_resp3_unified, 

107 parse_stralgo_unified, 

108 parse_stream_list, 

109 parse_xautoclaim, 

110 parse_xclaim, 

111 parse_xinfo_stream, 

112 parse_xpending, 

113 parse_xread, 

114 parse_xread_resp3, 

115 parse_xread_resp3_to_resp2_legacy, 

116 parse_xread_unified, 

117 parse_zadd, 

118 parse_zmove, 

119 parse_zmscore, 

120 parse_zscan, 

121 parse_zscan_unified, 

122 sort_return_tuples, 

123 string_keys_to_dict, 

124 timestamp_to_datetime, 

125 zmpop_resp3_to_resp2_legacy, 

126 zmpop_unified, 

127 zpop_score_pairs, 

128 zpop_score_pairs_resp3_to_resp2_legacy, 

129 zpop_score_pairs_resp3_unified, 

130 zpop_score_pairs_unified, 

131 zset_score_for_rank, 

132 zset_score_for_rank_resp3, 

133 zset_score_for_rank_resp3_to_resp2_legacy, 

134 zset_score_for_rank_unified, 

135 zset_score_pairs, 

136 zset_score_pairs_resp3, 

137 zset_score_pairs_resp3_to_resp2_legacy, 

138 zset_score_pairs_resp3_to_resp2_legacy_flat, 

139 zset_score_pairs_unified, 

140) 

141 

142_RedisCallbacks = { 

143 **string_keys_to_dict( 

144 "AUTH COPY EXPIRE EXPIREAT HEXISTS HMSET MOVE MSETNX PERSIST PSETEX " 

145 "PEXPIRE PEXPIREAT RENAMENX SETEX SETNX SMOVE", 

146 bool, 

147 ), 

148 **string_keys_to_dict("HINCRBYFLOAT INCRBYFLOAT", float), 

149 **string_keys_to_dict( 

150 "ASKING FLUSHALL FLUSHDB LSET LTRIM MSET PFMERGE READONLY READWRITE " 

151 "RENAME SAVE SELECT SHUTDOWN SLAVEOF SWAPDB WATCH UNWATCH", 

152 bool_ok, 

153 ), 

154 **string_keys_to_dict("XREAD XREADGROUP", parse_xread), 

155 **string_keys_to_dict( 

156 "GEORADIUS GEORADIUSBYMEMBER GEOSEARCH", 

157 parse_geosearch_generic, 

158 ), 

159 **string_keys_to_dict("XRANGE XREVRANGE", parse_stream_list), 

160 "ACL GETUSER": parse_acl_getuser, 

161 "ACL LOAD": bool_ok, 

162 "ACL LOG": parse_acl_log, 

163 "ACL SETUSER": bool_ok, 

164 "ACL SAVE": bool_ok, 

165 "BLESS SCAN": parse_scan, 

166 "CLIENT INFO": parse_client_info, 

167 "CLIENT KILL": parse_client_kill, 

168 "CLIENT LIST": parse_client_list, 

169 "CLIENT PAUSE": bool_ok, 

170 "CLIENT SETINFO": bool_ok, 

171 "CLIENT SETNAME": bool_ok, 

172 "CLIENT UNBLOCK": bool, 

173 "CLUSTER ADDSLOTS": bool_ok, 

174 "CLUSTER ADDSLOTSRANGE": bool_ok, 

175 "CLUSTER DELSLOTS": bool_ok, 

176 "CLUSTER DELSLOTSRANGE": bool_ok, 

177 "CLUSTER FAILOVER": bool_ok, 

178 "CLUSTER FORGET": bool_ok, 

179 "CLUSTER INFO": parse_cluster_info, 

180 "CLUSTER MEET": bool_ok, 

181 "CLUSTER NODES": parse_cluster_nodes, 

182 "CLUSTER REPLICAS": parse_cluster_nodes, 

183 "CLUSTER REPLICATE": bool_ok, 

184 "CLUSTER RESET": bool_ok, 

185 "CLUSTER SAVECONFIG": bool_ok, 

186 "CLUSTER SET-CONFIG-EPOCH": bool_ok, 

187 "CLUSTER SETSLOT": bool_ok, 

188 "CLUSTER SLAVES": parse_cluster_nodes, 

189 "COMMAND": parse_command, 

190 "CONFIG RESETSTAT": bool_ok, 

191 "CONFIG SET": bool_ok, 

192 "FUNCTION DELETE": bool_ok, 

193 "FUNCTION FLUSH": bool_ok, 

194 "FUNCTION RESTORE": bool_ok, 

195 "GEODIST": float_or_none, 

196 HIMPORT_PREPARE: bool_ok, 

197 HIMPORT_SET: bool_ok, 

198 HIMPORT_DISCARD: int, 

199 HIMPORT_DISCARDALL: int, 

200 "HSCAN": parse_hscan, 

201 "INFO": parse_info, 

202 "LASTSAVE": timestamp_to_datetime, 

203 "MEMORY PURGE": bool_ok, 

204 "MODULE LOAD": bool, 

205 "MODULE UNLOAD": bool, 

206 "PING": lambda r: str_if_bytes(r) == "PONG", 

207 "PUBSUB NUMSUB": parse_pubsub_numsub, 

208 "PUBSUB SHARDNUMSUB": parse_pubsub_numsub, 

209 "QUIT": bool_ok, 

210 "SET": parse_set_result, 

211 "SCAN": parse_scan, 

212 "SCRIPT EXISTS": lambda r: list(map(bool, r)), 

213 "SCRIPT FLUSH": bool_ok, 

214 "SCRIPT KILL": bool_ok, 

215 "SCRIPT LOAD": str_if_bytes, 

216 "SENTINEL CKQUORUM": bool_ok, 

217 "SENTINEL FAILOVER": bool_ok, 

218 "SENTINEL FLUSHCONFIG": bool_ok, 

219 "SENTINEL GET-MASTER-ADDR-BY-NAME": parse_sentinel_get_master, 

220 "SENTINEL MONITOR": bool_ok, 

221 "SENTINEL RESET": bool_ok, 

222 "SENTINEL REMOVE": bool_ok, 

223 "SENTINEL SET": bool_ok, 

224 "SLOWLOG GET": parse_slowlog_get, 

225 "SLOWLOG RESET": bool_ok, 

226 "SORT": sort_return_tuples, 

227 "SSCAN": parse_scan, 

228 "TIME": lambda x: (int(x[0]), int(x[1])), 

229 "XAUTOCLAIM": parse_xautoclaim, 

230 "XCLAIM": parse_xclaim, 

231 "XGROUP CREATE": bool_ok, 

232 "XGROUP DESTROY": bool, 

233 "XGROUP SETID": bool_ok, 

234 "ARINFO": parse_arinfo, 

235 "XINFO STREAM": parse_xinfo_stream, 

236 "XPENDING": parse_xpending, 

237 "ZSCAN": parse_zscan, 

238} 

239 

240 

241_RedisCallbacksRESP2 = { 

242 **string_keys_to_dict( 

243 "SDIFF SINTER SMEMBERS SUNION", lambda r: r and set(r) or set() 

244 ), 

245 **string_keys_to_dict( 

246 "ZINTER ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE ZUNION", 

247 zset_score_pairs, 

248 ), 

249 **string_keys_to_dict("ZPOPMAX ZPOPMIN", zpop_score_pairs), 

250 **string_keys_to_dict("BZMOVE ZMOVE ZMOVEMEMBER", parse_zmove), 

251 **string_keys_to_dict( 

252 "ZREVRANK ZRANK", 

253 zset_score_for_rank, 

254 ), 

255 **string_keys_to_dict("ZINCRBY ZSCORE", float_or_none), 

256 **string_keys_to_dict("BGREWRITEAOF BGSAVE", lambda r: True), 

257 **string_keys_to_dict("BLPOP BRPOP", lambda r: r and tuple(r) or None), 

258 **string_keys_to_dict( 

259 "BZPOPMAX BZPOPMIN", lambda r: r and (r[0], r[1], float(r[2])) or None 

260 ), 

261 "ACL CAT": lambda r: list(map(str_if_bytes, r)), 

262 "ACL GENPASS": str_if_bytes, 

263 "ACL HELP": lambda r: list(map(str_if_bytes, r)), 

264 "ACL LIST": lambda r: list(map(str_if_bytes, r)), 

265 "ACL USERS": lambda r: list(map(str_if_bytes, r)), 

266 "ACL WHOAMI": str_if_bytes, 

267 "BACKUP STATUS": pairs_to_dict, 

268 "CLIENT GETNAME": str_if_bytes, 

269 "CLIENT TRACKINGINFO": lambda r: list(map(str_if_bytes, r)), 

270 "CLUSTER GETKEYSINSLOT": lambda r: list(map(str_if_bytes, r)), 

271 "COMMAND GETKEYS": lambda r: list(map(str_if_bytes, r)), 

272 "CONFIG GET": parse_config_get, 

273 "DEBUG OBJECT": parse_debug_object, 

274 "GEOHASH": lambda r: list(map(str_if_bytes, r)), 

275 "GEOPOS": lambda r: list( 

276 map(lambda ll: (float(ll[0]), float(ll[1])) if ll is not None else None, r) 

277 ), 

278 "HGETALL": lambda r: r and pairs_to_dict(r) or {}, 

279 "HOTKEYS GET": lambda r: None if r is None else [pairs_to_dict(m) for m in r], 

280 "MEMORY STATS": parse_memory_stats, 

281 "MODULE LIST": lambda r: [pairs_to_dict(m) for m in r], 

282 "RESET": str_if_bytes, 

283 "SENTINEL MASTER": parse_sentinel_master, 

284 "SENTINEL MASTERS": parse_sentinel_masters, 

285 "SENTINEL SENTINELS": parse_sentinel_slaves_and_sentinels, 

286 "SENTINEL SLAVES": parse_sentinel_slaves_and_sentinels, 

287 "STRALGO": parse_stralgo, 

288 "XINFO CONSUMERS": parse_list_of_dicts, 

289 "XINFO GROUPS": parse_list_of_dicts, 

290 "ZADD": parse_zadd, 

291 "ZMSCORE": parse_zmscore, 

292} 

293 

294 

295_RedisCallbacksRESP3 = { 

296 **string_keys_to_dict( 

297 "SDIFF SINTER SMEMBERS SUNION", lambda r: r and set(r) or set() 

298 ), 

299 **string_keys_to_dict( 

300 "ZRANGE ZINTER ZPOPMAX ZPOPMIN HGETALL XREADGROUP", 

301 lambda r, **kwargs: r, 

302 ), 

303 **string_keys_to_dict( 

304 "ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE ZUNION", 

305 zset_score_pairs_resp3, 

306 ), 

307 **string_keys_to_dict( 

308 "ZREVRANK ZRANK", 

309 zset_score_for_rank_resp3, 

310 ), 

311 **string_keys_to_dict("XREAD XREADGROUP", parse_xread_resp3), 

312 "ACL LOG": lambda r: ( 

313 [ 

314 {str_if_bytes(key): str_if_bytes(value) for key, value in x.items()} 

315 for x in r 

316 ] 

317 if isinstance(r, list) 

318 else bool_ok(r) 

319 ), 

320 "COMMAND": parse_command_resp3, 

321 "CONFIG GET": parse_config_get_resp3_to_resp2_legacy, 

322 "MEMORY STATS": parse_memory_stats_resp3, 

323 "SENTINEL MASTER": parse_sentinel_state_resp3, 

324 "SENTINEL MASTERS": parse_sentinel_masters_resp3, 

325 "SENTINEL SENTINELS": parse_sentinel_slaves_and_sentinels_resp3, 

326 "SENTINEL SLAVES": parse_sentinel_slaves_and_sentinels_resp3, 

327 "STRALGO": lambda r, **options: ( 

328 {str_if_bytes(key): str_if_bytes(value) for key, value in r.items()} 

329 if isinstance(r, dict) 

330 else str_if_bytes(r) 

331 ), 

332 "XINFO CONSUMERS": parse_list_of_dicts_resp3, 

333 "XINFO GROUPS": parse_list_of_dicts_resp3, 

334} 

335 

336 

337# RESP2 wire, unified response shapes (``legacy_responses=False``). 

338_RedisCallbacksRESP2Unified: dict[str, Callable[..., Any]] = { 

339 **_RedisCallbacksRESP2, 

340 **string_keys_to_dict( 

341 "ZDIFF ZINTER ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE ZUNION", 

342 zset_score_pairs_unified, 

343 ), 

344 **string_keys_to_dict( 

345 "ZPOPMAX ZPOPMIN", 

346 zpop_score_pairs_unified, 

347 ), 

348 **string_keys_to_dict( 

349 "ZREVRANK ZRANK", 

350 zset_score_for_rank_unified, 

351 ), 

352 **string_keys_to_dict( 

353 "BZPOPMAX BZPOPMIN", 

354 bzpop_score_unified, 

355 ), 

356 **string_keys_to_dict("XREAD XREADGROUP", parse_xread_unified), 

357 **string_keys_to_dict( 

358 "GEORADIUS GEORADIUSBYMEMBER GEOSEARCH", 

359 parse_geosearch_generic_unified, 

360 ), 

361 **string_keys_to_dict("BLPOP BRPOP", lambda r: r or None), 

362 **string_keys_to_dict("ZMPOP BZMPOP", zmpop_unified), 

363 "ZSCAN": parse_zscan_unified, 

364 "ZRANDMEMBER": zset_score_pairs_unified, 

365 "HRANDFIELD": hrandfield_unified, 

366 "ACL GETUSER": parse_acl_getuser_unified, 

367 "CLIENT TRACKINGINFO": parse_client_trackinginfo_unified, 

368 "CLUSTER GETKEYSINSLOT": lambda r, **kwargs: r, 

369 "CLUSTER LINKS": parse_cluster_links_unified, 

370 "COMMAND": parse_command_unified, 

371 "COMMAND GETKEYS": lambda r, **kwargs: r, 

372 "FUNCTION LIST": parse_function_list_unified, 

373 "FUNCTION STATS": parse_function_stats_unified, 

374 "GEOPOS": parse_geopos_unified, 

375 "INCREX": parse_increx_unified, 

376 "LCS": parse_lcs_idx_unified, 

377 "MEMORY STATS": parse_memory_stats_unified, 

378 "SENTINEL MASTER": parse_sentinel_master_unified, 

379 "SENTINEL MASTERS": parse_sentinel_masters_unified, 

380 "SENTINEL SENTINELS": parse_sentinel_slaves_and_sentinels_unified, 

381 "SENTINEL SLAVES": parse_sentinel_slaves_and_sentinels_unified, 

382 "STRALGO": parse_stralgo_unified, 

383} 

384 

385 

386# RESP3 wire, unified response shapes (``legacy_responses=False``). 

387_RedisCallbacksRESP3Unified: dict[str, Callable[..., Any]] = { 

388 **_RedisCallbacksRESP3, 

389 **string_keys_to_dict( 

390 "ZDIFF ZINTER ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE ZUNION", 

391 zset_score_pairs_resp3, 

392 ), 

393 **string_keys_to_dict( 

394 "ZPOPMAX ZPOPMIN", 

395 zpop_score_pairs_resp3_unified, 

396 ), 

397 **string_keys_to_dict( 

398 "BZPOPMAX BZPOPMIN", 

399 bzpop_score_unified, 

400 ), 

401 **string_keys_to_dict("XREAD XREADGROUP", parse_xread_unified), 

402 **string_keys_to_dict( 

403 "GEORADIUS GEORADIUSBYMEMBER GEOSEARCH", 

404 parse_geosearch_generic_unified, 

405 ), 

406 **string_keys_to_dict("BGREWRITEAOF BGSAVE", lambda r: True), 

407 "ZSCAN": parse_zscan_unified, 

408 "ZRANDMEMBER": zset_score_pairs_resp3, 

409 "ACL CAT": lambda r: list(map(str_if_bytes, r)), 

410 "ACL GENPASS": str_if_bytes, 

411 "ACL HELP": lambda r: list(map(str_if_bytes, r)), 

412 "ACL LIST": lambda r: list(map(str_if_bytes, r)), 

413 "ACL LOG": parse_acl_log_resp3_unified, 

414 "ACL USERS": lambda r: list(map(str_if_bytes, r)), 

415 "ACL WHOAMI": str_if_bytes, 

416 "CLIENT GETNAME": str_if_bytes, 

417 "CLIENT TRACKINGINFO": parse_client_trackinginfo_unified, 

418 "CLUSTER LINKS": parse_cluster_links_unified, 

419 "COMMAND": parse_command_unified, 

420 "DEBUG OBJECT": parse_debug_object, 

421 "FUNCTION LIST": parse_function_list_unified, 

422 "FUNCTION STATS": parse_function_stats_unified, 

423 "GEOHASH": lambda r: list(map(str_if_bytes, r)), 

424 "LCS": parse_lcs_idx_unified, 

425 "RESET": str_if_bytes, 

426 "SENTINEL MASTER": parse_sentinel_master_unified_resp3, 

427 "SENTINEL MASTERS": parse_sentinel_masters_unified_resp3, 

428 "SENTINEL SENTINELS": parse_sentinel_slaves_and_sentinels_unified_resp3, 

429 "SENTINEL SLAVES": parse_sentinel_slaves_and_sentinels_unified_resp3, 

430 "STRALGO": parse_stralgo_resp3_unified, 

431} 

432 

433 

434# RESP3 wire converted back to the legacy RESP2 Python shapes. Only the 

435# entries needed to undo RESP3-side differences are listed; everything 

436# else falls through to ``_RedisCallbacks``. Scores are re-encoded to bytes 

437# (``str`` when the client decodes responses) before being passed to 

438# ``score_cast_func`` so the callable observes the same input type it would 

439# on a RESP2 connection. 

440_RedisCallbacksRESP3toRESP2Legacy: dict[str, Callable[..., Any]] = { 

441 **string_keys_to_dict( 

442 "SDIFF SINTER SMEMBERS SUNION", lambda r: r and set(r) or set() 

443 ), 

444 **string_keys_to_dict( 

445 "ZINTER ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE ZUNION", 

446 zset_score_pairs_resp3_to_resp2_legacy, 

447 ), 

448 "ZDIFF": zset_score_pairs_resp3_to_resp2_legacy_flat, 

449 "ZRANDMEMBER": zset_score_pairs_resp3_to_resp2_legacy_flat, 

450 **string_keys_to_dict( 

451 "ZPOPMAX ZPOPMIN", 

452 zpop_score_pairs_resp3_to_resp2_legacy, 

453 ), 

454 **string_keys_to_dict( 

455 "BZPOPMAX BZPOPMIN", 

456 bzpop_score_resp3_to_resp2_legacy, 

457 ), 

458 **string_keys_to_dict( 

459 "ZREVRANK ZRANK", 

460 zset_score_for_rank_resp3_to_resp2_legacy, 

461 ), 

462 **string_keys_to_dict("BGREWRITEAOF BGSAVE", lambda r: True), 

463 **string_keys_to_dict("XREAD XREADGROUP", parse_xread_resp3_to_resp2_legacy), 

464 **string_keys_to_dict("BLPOP BRPOP", lambda r: r and tuple(r) or None), 

465 **string_keys_to_dict("ZMPOP BZMPOP", zmpop_resp3_to_resp2_legacy), 

466 "HRANDFIELD": hrandfield_resp3_to_resp2_legacy, 

467 "ACL CAT": lambda r: list(map(str_if_bytes, r)), 

468 "ACL GENPASS": str_if_bytes, 

469 "ACL GETUSER": parse_acl_getuser_resp3_to_resp2_legacy, 

470 "ACL HELP": lambda r: list(map(str_if_bytes, r)), 

471 "ACL LIST": lambda r: list(map(str_if_bytes, r)), 

472 "ACL LOG": parse_acl_log_resp3_to_resp2_legacy, 

473 "ACL USERS": lambda r: list(map(str_if_bytes, r)), 

474 "ACL WHOAMI": str_if_bytes, 

475 "CLIENT GETNAME": str_if_bytes, 

476 "CLIENT TRACKINGINFO": parse_client_trackinginfo_resp3_to_resp2_legacy, 

477 "CLUSTER GETKEYSINSLOT": lambda r: list(map(str_if_bytes, r)), 

478 "CLUSTER LINKS": parse_cluster_links_resp3_to_resp2_legacy, 

479 "COMMAND GETKEYS": lambda r: list(map(str_if_bytes, r)), 

480 "CONFIG GET": parse_config_get_resp3_to_resp2_legacy, 

481 "DEBUG OBJECT": parse_debug_object, 

482 "FUNCTION LIST": parse_function_list_resp3_to_resp2_legacy, 

483 "FUNCTION STATS": parse_function_stats_resp3_to_resp2_legacy, 

484 "GEOHASH": lambda r: list(map(str_if_bytes, r)), 

485 "GEOPOS": parse_geopos_resp3_to_resp2_legacy, 

486 "LCS": parse_lcs_idx_resp3_to_resp2_legacy, 

487 "MEMORY STATS": parse_memory_stats_resp3, 

488 "RESET": str_if_bytes, 

489 "SENTINEL MASTER": parse_sentinel_master_resp3_to_resp2_legacy, 

490 "SENTINEL MASTERS": parse_sentinel_masters_resp3_to_resp2_legacy, 

491 "SENTINEL SENTINELS": parse_sentinel_slaves_and_sentinels_resp3_to_resp2_legacy, 

492 "SENTINEL SLAVES": parse_sentinel_slaves_and_sentinels_resp3_to_resp2_legacy, 

493 "XINFO CONSUMERS": parse_list_of_dicts_resp3, 

494 "XINFO GROUPS": parse_list_of_dicts_resp3, 

495} 

496 

497 

498def get_response_callbacks( 

499 user_protocol: Optional[int], 

500 legacy_responses: bool, 

501) -> dict[str, Callable[..., Any]]: 

502 """Return the merged callback dict for the given (protocol, legacy) 

503 combination. 

504 

505 ``user_protocol`` is the value the user supplied to the client 

506 constructor (``None`` means "not specified"). ``legacy_responses`` 

507 defaults to ``True`` and selects today's RESP2-style Python shapes 

508 even when the wire protocol is RESP3. 

509 

510 Callers wrap the returned dict in ``CaseInsensitiveDict``. 

511 """ 

512 callbacks: dict[str, Callable[..., Any]] = dict(_RedisCallbacks) 

513 if legacy_responses: 

514 if user_protocol is None: 

515 callbacks.update(_RedisCallbacksRESP3toRESP2Legacy) 

516 elif user_protocol in (3, "3"): 

517 callbacks.update(_RedisCallbacksRESP3) 

518 else: 

519 callbacks.update(_RedisCallbacksRESP2) 

520 else: 

521 if user_protocol is None or user_protocol in (3, "3"): 

522 callbacks.update(_RedisCallbacksRESP3Unified) 

523 else: 

524 callbacks.update(_RedisCallbacksRESP2Unified) 

525 return callbacks 

526 

527 

528def _get_response_callback( 

529 callbacks: Mapping, command: str | bytes, connection: Any 

530) -> Callable | None: 

531 """Select a default callback for the negotiated wire protocol. 

532 

533 A default-RESP3 connection may fall back independently of the other 

534 connections using this mapping. Select its RESP2 callback without mutating 

535 the shared mapping or replacing callbacks installed by the caller. 

536 """ 

537 if not getattr(connection, "_resp2_fallback", False): 

538 return callbacks.get(command) 

539 

540 normalized_command = str_if_bytes(command).upper() 

541 callback = callbacks.get(normalized_command) 

542 if command != normalized_command: 

543 # Preserve callbacks registered under the caller's original spelling. 

544 callback = callbacks.get(command, callback) 

545 if connection.legacy_responses: 

546 resp3_callbacks = _RedisCallbacksRESP3toRESP2Legacy 

547 resp2_callbacks = _RedisCallbacksRESP2 

548 else: 

549 resp3_callbacks = _RedisCallbacksRESP3Unified 

550 resp2_callbacks = _RedisCallbacksRESP2Unified 

551 default_callback = resp3_callbacks.get( 

552 normalized_command, _RedisCallbacks.get(normalized_command) 

553 ) 

554 if callback is default_callback: 

555 return resp2_callbacks.get( 

556 normalized_command, _RedisCallbacks.get(normalized_command) 

557 ) 

558 return callback