From 2582cc6c013a7c8d98c22f537a0dab99dbd7cf54 Mon Sep 17 00:00:00 2001 From: Petya Slavova Date: Fri, 4 Apr 2025 10:44:18 +0300 Subject: [PATCH] Handling some special values when transforming responce data into list(issue #3573) --- redis/commands/helpers.py | 25 +++++++++++++++++++------ tests/test_asyncio/test_bloom.py | 28 ++++++++++++++++++++++++++++ tests/test_bloom.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/redis/commands/helpers.py b/redis/commands/helpers.py index f6121b6c3b..859a43aea9 100644 --- a/redis/commands/helpers.py +++ b/redis/commands/helpers.py @@ -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 diff --git a/tests/test_asyncio/test_bloom.py b/tests/test_asyncio/test_bloom.py index 031e8364d7..d67858570f 100644 --- a/tests/test_asyncio/test_bloom.py +++ b/tests/test_asyncio/test_bloom.py @@ -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() diff --git a/tests/test_bloom.py b/tests/test_bloom.py index e44c421634..a8d6390048 100644 --- a/tests/test_bloom.py +++ b/tests/test_bloom.py @@ -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()