Skip to content

Handling some special values when transforming response data into list(issue #3573) #3586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions redis/commands/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,32 @@ def parse_to_list(response):
"""Optimistically parse the response to a list."""
res = []

special_values = {"infinity", "nan", "-infinity"}

if response is None:
return res

for item in response:
if item is None:
res.append(None)
continue
try:
res.append(int(item))
except ValueError:
try:
res.append(float(item))
except ValueError:
res.append(nativestr(item))
item_str = nativestr(item)
except TypeError:
res.append(None)
continue

if isinstance(item_str, str) and item_str.lower() in special_values:
res.append(item_str) # Keep as string
else:
try:
res.append(int(item))
except ValueError:
try:
res.append(float(item))
except ValueError:
res.append(item_str)

return res


Expand Down
28 changes: 28 additions & 0 deletions tests/test_asyncio/test_bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,34 @@ async def test_topk(decoded_r: redis.Redis):
assert 0.9 == round(float(info["decay"]), 1)


@pytest.mark.redismod
async def test_topk_list_with_special_words(decoded_r: redis.Redis):
# test list with empty buckets
assert await decoded_r.topk().reserve("topklist:specialwords", 5, 20, 4, 0.9)
assert await decoded_r.topk().add(
"topklist:specialwords",
"infinity",
"B",
"nan",
"D",
"-infinity",
"infinity",
"infinity",
"B",
"nan",
"G",
"D",
"B",
"D",
"infinity",
"-infinity",
"-infinity",
)
assert ["infinity", "B", "D", "-infinity", "nan"] == await decoded_r.topk().list(
"topklist:specialwords"
)


@pytest.mark.redismod
async def test_topk_incrby(decoded_r: redis.Redis):
await decoded_r.flushdb()
Expand Down
28 changes: 28 additions & 0 deletions tests/test_bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,34 @@ def test_topk(client):
assert 0.9 == round(float(info["decay"]), 1)


@pytest.mark.redismod
def test_topk_list_with_special_words(client):
# test list with empty buckets
assert client.topk().reserve("topklist:specialwords", 5, 20, 4, 0.9)
assert client.topk().add(
"topklist:specialwords",
"infinity",
"B",
"nan",
"D",
"-infinity",
"infinity",
"infinity",
"B",
"nan",
"G",
"D",
"B",
"D",
"infinity",
"-infinity",
"-infinity",
)
assert ["infinity", "B", "D", "-infinity", "nan"] == client.topk().list(
"topklist:specialwords"
)


@pytest.mark.redismod
def test_topk_incrby(client):
client.flushdb()
Expand Down
Loading