Skip to content

Commit 078f96f

Browse files
committed
Handling some special values when transforming responce data into list(issue #3573) (#3586)
1 parent fe13ed1 commit 078f96f

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

redis/commands/helpers.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,32 @@ def parse_to_list(response):
4343
"""Optimistically parse the response to a list."""
4444
res = []
4545

46+
special_values = {"infinity", "nan", "-infinity"}
47+
4648
if response is None:
4749
return res
4850

4951
for item in response:
52+
if item is None:
53+
res.append(None)
54+
continue
5055
try:
51-
res.append(int(item))
52-
except ValueError:
53-
try:
54-
res.append(float(item))
55-
except ValueError:
56-
res.append(nativestr(item))
56+
item_str = nativestr(item)
5757
except TypeError:
5858
res.append(None)
59+
continue
60+
61+
if isinstance(item_str, str) and item_str.lower() in special_values:
62+
res.append(item_str) # Keep as string
63+
else:
64+
try:
65+
res.append(int(item))
66+
except ValueError:
67+
try:
68+
res.append(float(item))
69+
except ValueError:
70+
res.append(item_str)
71+
5972
return res
6073

6174

tests/test_asyncio/test_bloom.py

+28
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,34 @@ async def test_topk(decoded_r: redis.Redis):
333333
assert 0.9 == round(float(info["decay"]), 1)
334334

335335

336+
@pytest.mark.redismod
337+
async def test_topk_list_with_special_words(decoded_r: redis.Redis):
338+
# test list with empty buckets
339+
assert await decoded_r.topk().reserve("topklist:specialwords", 5, 20, 4, 0.9)
340+
assert await decoded_r.topk().add(
341+
"topklist:specialwords",
342+
"infinity",
343+
"B",
344+
"nan",
345+
"D",
346+
"-infinity",
347+
"infinity",
348+
"infinity",
349+
"B",
350+
"nan",
351+
"G",
352+
"D",
353+
"B",
354+
"D",
355+
"infinity",
356+
"-infinity",
357+
"-infinity",
358+
)
359+
assert ["infinity", "B", "D", "-infinity", "nan"] == await decoded_r.topk().list(
360+
"topklist:specialwords"
361+
)
362+
363+
336364
@pytest.mark.redismod
337365
async def test_topk_incrby(decoded_r: redis.Redis):
338366
await decoded_r.flushdb()

tests/test_bloom.py

+28
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,34 @@ def test_topk(client):
364364
assert 0.9 == round(float(info["decay"]), 1)
365365

366366

367+
@pytest.mark.redismod
368+
def test_topk_list_with_special_words(client):
369+
# test list with empty buckets
370+
assert client.topk().reserve("topklist:specialwords", 5, 20, 4, 0.9)
371+
assert client.topk().add(
372+
"topklist:specialwords",
373+
"infinity",
374+
"B",
375+
"nan",
376+
"D",
377+
"-infinity",
378+
"infinity",
379+
"infinity",
380+
"B",
381+
"nan",
382+
"G",
383+
"D",
384+
"B",
385+
"D",
386+
"infinity",
387+
"-infinity",
388+
"-infinity",
389+
)
390+
assert ["infinity", "B", "D", "-infinity", "nan"] == client.topk().list(
391+
"topklist:specialwords"
392+
)
393+
394+
367395
@pytest.mark.redismod
368396
def test_topk_incrby(client):
369397
client.flushdb()

0 commit comments

Comments
 (0)