Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Add items to signature of hset #1300

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGES/1299.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow passing `items` to `hset`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Kyrylo Dehtyarenko
Leonid Shvechikov
Maksim Novikov
Manuel Miranda
Marek Czaplicki
Marek Szapiel
Marijn Giesen
Martin <the-panda>
Expand Down
9 changes: 6 additions & 3 deletions aioredis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3497,16 +3497,19 @@ def hset(
key: Optional[FieldT] = None,
value: Optional[EncodableT] = None,
mapping: Optional[Mapping[AnyFieldT, EncodableT]] = None,
items: Optional[List[Union[FieldT, Optional[EncodableT]]]] = None,
) -> Awaitable:
"""
Set ``key`` to ``value`` within hash ``name``,
``mapping`` accepts a dict of key/value pairs that that will be
``mapping`` accepts a dict of key/value pairs that will be
added to hash ``name``.
``items`` accepts a list of key/value pairs that will be
added to hash ``name``.
Returns the number of fields that were added.
"""
if key is None and not mapping:
if key is None and not mapping and not items:
raise DataError("'hset' with no key value pairs")
items: List[Union[FieldT, Optional[EncodableT]]] = []
items: List[Union[FieldT, Optional[EncodableT]]] = items or []
if key is not None:
items.extend((key, value))
if mapping:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,17 @@ async def test_hset_with_multi_key_values(self, r: aioredis.Redis):
assert await r.hget("b", "2") == b"2"
assert await r.hget("b", "foo") == b"bar"

async def test_hset_with_items(self, r: aioredis.Redis):
await r.hset("a", items=["1", 1, "2", 2, "3", 3]
assert await r.hget("a", "1") == b"1"
assert await r.hget("a", "2") == b"2"
assert await r.hget("a", "3") == b"3"

await r.hset("b", "foo", "bar", items=["1", 1, "2", 2])
assert await r.hget("b", "1") == b"1"
assert await r.hget("b", "2") == b"2"
assert await r.hget("b", "foo") == b"bar"

async def test_hset_without_data(self, r: aioredis.Redis):
with pytest.raises(exceptions.DataError):
await r.hset("x")
Expand Down