Skip to content
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

Add type hints for hash commands #1919

Merged
merged 4 commits into from
Feb 3, 2022
Merged
Changes from 2 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
35 changes: 21 additions & 14 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hashlib
import time
import warnings
from typing import List, Optional

from redis.exceptions import ConnectionError, DataError, NoScriptError, RedisError

Expand Down Expand Up @@ -3676,71 +3677,77 @@ class HashCommands:
see: https://redis.io/topics/data-types-intro#redis-hashes
"""

def hdel(self, name, *keys):
def hdel(self, name: str, *keys: List) -> int:
"""
Delete ``keys`` from hash ``name``

For more information check https://redis.io/commands/hdel
"""
return self.execute_command("HDEL", name, *keys)

def hexists(self, name, key):
def hexists(self, name: str, key: str) -> bool:
"""
Returns a boolean indicating if ``key`` exists within hash ``name``

For more information check https://redis.io/commands/hexists
"""
return self.execute_command("HEXISTS", name, key)

def hget(self, name, key):
def hget(self, name: str, key: str) -> Optional[str]:
"""
Return the value of ``key`` within the hash ``name``

For more information check https://redis.io/commands/hget
"""
return self.execute_command("HGET", name, key)

def hgetall(self, name):
def hgetall(self, name: str) -> dict:
"""
Return a Python dict of the hash's name/value pairs

For more information check https://redis.io/commands/hgetall
"""
return self.execute_command("HGETALL", name)

def hincrby(self, name, key, amount=1):
def hincrby(self, name: str, key: str, amount: int = 1) -> int:
"""
Increment the value of ``key`` in hash ``name`` by ``amount``

For more information check https://redis.io/commands/hincrby
"""
return self.execute_command("HINCRBY", name, key, amount)

def hincrbyfloat(self, name, key, amount=1.0):
def hincrbyfloat(self, name: str, key: str, amount: float = 1.0) -> float:
"""
Increment the value of ``key`` in hash ``name`` by floating ``amount``

For more information check https://redis.io/commands/hincrbyfloat
"""
return self.execute_command("HINCRBYFLOAT", name, key, amount)

def hkeys(self, name):
def hkeys(self, name: str) -> List:
"""
Return the list of keys within hash ``name``

For more information check https://redis.io/commands/hkeys
"""
return self.execute_command("HKEYS", name)

def hlen(self, name):
def hlen(self, name: str) -> int:
"""
Return the number of elements in hash ``name``

For more information check https://redis.io/commands/hlen
"""
return self.execute_command("HLEN", name)

def hset(self, name, key=None, value=None, mapping=None):
def hset(
self,
name: str,
key: Optional[str] = None,
value: Optional[str] = None,
mapping: Optional[dict] = None,
) -> int:
"""
Set ``key`` to ``value`` within hash ``name``,
``mapping`` accepts a dict of key/value pairs that will be
Expand All @@ -3760,7 +3767,7 @@ def hset(self, name, key=None, value=None, mapping=None):

return self.execute_command("HSET", name, *items)

def hsetnx(self, name, key, value):
def hsetnx(self, name: str, key: str, value: str) -> bool:
"""
Set ``key`` to ``value`` within hash ``name`` if ``key`` does not
exist. Returns 1 if HSETNX created a field, otherwise 0.
Expand All @@ -3769,7 +3776,7 @@ def hsetnx(self, name, key, value):
"""
return self.execute_command("HSETNX", name, key, value)

def hmset(self, name, mapping):
def hmset(self, name: str, mapping: dict) -> str:
"""
Set key to value within hash ``name`` for each corresponding
key and value from the ``mapping`` dict.
Expand All @@ -3789,7 +3796,7 @@ def hmset(self, name, mapping):
items.extend(pair)
return self.execute_command("HMSET", name, *items)

def hmget(self, name, keys, *args):
def hmget(self, name: str, keys: List, *args: List) -> List:
"""
Returns a list of values ordered identically to ``keys``

Expand All @@ -3798,15 +3805,15 @@ def hmget(self, name, keys, *args):
args = list_or_args(keys, args)
return self.execute_command("HMGET", name, *args)

def hvals(self, name):
def hvals(self, name: str) -> List:
"""
Return the list of values within hash ``name``

For more information check https://redis.io/commands/hvals
"""
return self.execute_command("HVALS", name)

def hstrlen(self, name, key):
def hstrlen(self, name: str, key: str) -> int:
"""
Return the number of bytes stored in the value of ``key``
within hash ``name``
Expand Down