Skip to content

Commit d63088a

Browse files
committed
trying to getitem on a key that doesn't exist should raise a KeyError
1 parent 2eb518a commit d63088a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

redis/client.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,20 @@ def expireat(self, name, when):
443443

444444
def get(self, name):
445445
"""
446-
Return the value at key ``name``, or None of the key doesn't exist
446+
Return the value at key ``name``, or None if the key doesn't exist
447447
"""
448448
return self.execute_command('GET', name)
449-
__getitem__ = get
449+
450+
def __getitem__(self, name):
451+
"""
452+
Return the value at key ``name``, raises a KeyError if the key
453+
doesn't exist.
454+
"""
455+
_name = self.get(name)
456+
if _name:
457+
return _name
458+
else:
459+
raise KeyError(name)
450460

451461
def getbit(self, name, offset):
452462
"Returns a boolean indicating the value of ``offset`` in ``name``"

0 commit comments

Comments
 (0)