Skip to content

Commit 6285275

Browse files
Saurav SharmaSaurav Sharma
authored andcommitted
Fix for DEFAULT_TIMEOUT for pexpire, unwanted comments removed and tescases & changelog updated
1 parent 9a38b1c commit 6285275

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

changelog.d/724.bugfix

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
Fix for Issue 724(expire method fails when using DEFAULT_TIMEOUT)
2-
3-
https://github.com/jazzband/django-redis/issues/724
1+
Hotfix for timeout=DEFAULT_TIMEOUT in expire and pexpire

django_redis/client/default.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,6 @@ def expire(
299299
version: Optional[int] = None,
300300
client: Optional[Redis] = None,
301301
) -> bool:
302-
# timeout could be of type int|timedelta
303-
# if timeout is DEFAULT_TIMEOUT or None then
304-
# use self._backend.default_timeout(django default cache timeout)
305-
306-
# for some strange reason mypy complains,
307-
# saying that timeout type is float | timedelta
308302
if (timeout is DEFAULT_TIMEOUT) or (timeout is None):
309303
timeout = self._backend.default_timeout # type: ignore
310304

@@ -324,6 +318,9 @@ def pexpire(
324318
version: Optional[int] = None,
325319
client: Optional[Redis] = None,
326320
) -> bool:
321+
if (timeout is DEFAULT_TIMEOUT) or (timeout is None):
322+
timeout = self._backend.default_timeout # type: ignore
323+
327324
if client is None:
328325
client = self.get_client(write=True)
329326

tests/test_backend.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ def test_expire(self, cache: RedisCache):
610610
def test_expire_with_default_timeout(self, cache: RedisCache):
611611
cache.set("foo", "bar", timeout=None)
612612
assert cache.expire("foo", DEFAULT_TIMEOUT) is True
613+
assert cache.expire("not-existent-key", DEFAULT_TIMEOUT) is False
613614

614615
def test_pexpire(self, cache: RedisCache):
615616
cache.set("foo", "bar", timeout=None)
@@ -619,6 +620,11 @@ def test_pexpire(self, cache: RedisCache):
619620
assert pytest.approx(ttl, 10) == 20500
620621
assert cache.pexpire("not-existent-key", 20500) is False
621622

623+
def test_pexpire_with_default_timeout(self, cache: RedisCache):
624+
cache.set("foo", "bar", timeout=None)
625+
assert cache.pexpire("foo", DEFAULT_TIMEOUT) is True
626+
assert cache.pexpire("not-existent-key", DEFAULT_TIMEOUT) is False
627+
622628
def test_pexpire_at(self, cache: RedisCache):
623629
# Test settings expiration time 1 hour ahead by datetime.
624630
cache.set("foo", "bar", timeout=None)

0 commit comments

Comments
 (0)