diff --git a/CHANGES/1299.feature b/CHANGES/1299.feature new file mode 100644 index 000000000..e224a73da --- /dev/null +++ b/CHANGES/1299.feature @@ -0,0 +1 @@ +Allow passing `items` to `hset`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 36953225a..88d12d0ed 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -42,6 +42,7 @@ Kyrylo Dehtyarenko Leonid Shvechikov Maksim Novikov Manuel Miranda +Marek Czaplicki Marek Szapiel Marijn Giesen Martin diff --git a/aioredis/client.py b/aioredis/client.py index 9b4e1a3d3..2c33cd9f6 100644 --- a/aioredis/client.py +++ b/aioredis/client.py @@ -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: diff --git a/tests/test_commands.py b/tests/test_commands.py index 51ab80738..5070f900a 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -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")