Skip to content

Commit c1eefbd

Browse files
authored
Return Redis sets as Python lists (#189)
In some (rare) cases, the sets from a Redis response contain nested types that are not hashable in Python, for example maps. To handle these cases uniformly, always return Python lists for Redis sets. The elements will still be unique, relying on the correctness of data arriving from the server. This is a breaking change, although in reality it might not have a big impact. Therefore the major version gets bumped to 3.
1 parent a94bb44 commit c1eefbd

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

hiredis/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.4.0"
1+
__version__ = "3.0.0"

src/reader.c

-7
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ static void *tryParentize(const redisReadTask *task, PyObject *obj) {
8282
PyDict_SetItem(parent, last_key, obj);
8383
}
8484
break;
85-
case REDIS_REPLY_SET:
86-
assert(PyAnySet_CheckExact(parent));
87-
PySet_Add(parent, obj);
88-
break;
8985
default:
9086
assert(PyList_CheckExact(parent));
9187
PyList_SET_ITEM(parent, task->idx, obj);
@@ -162,9 +158,6 @@ static void *createArrayObject(const redisReadTask *task, size_t elements) {
162158
case REDIS_REPLY_MAP:
163159
obj = PyDict_New();
164160
break;
165-
case REDIS_REPLY_SET:
166-
obj = PySet_New(NULL);
167-
break;
168161
default:
169162
obj = PyList_New(elements);
170163
}

tests/test_reader.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ def test_none(reader):
136136

137137
def test_set(reader):
138138
reader.feed(b"~3\r\n+tangerine\r\n_\r\n,10.5\r\n")
139-
assert {b"tangerine", None, 10.5} == reader.gets()
139+
assert [b"tangerine", None, 10.5] == reader.gets()
140+
141+
def test_set_with_nested_dict(reader):
142+
reader.feed(b"~2\r\n+tangerine\r\n%1\r\n+a\r\n:1\r\n")
143+
assert [b"tangerine", {b"a": 1}] == reader.gets()
140144

141145
def test_dict(reader):
142146
reader.feed(b"%2\r\n+radius\r\n,4.5\r\n+diameter\r\n:9\r\n")

0 commit comments

Comments
 (0)