Skip to content

Commit

Permalink
Fix for DEFAULT_TIMEOUT for pexpire, unwanted comments removed and te…
Browse files Browse the repository at this point in the history
…scases & changelog updated
  • Loading branch information
Saurav Sharma authored and Saurav Sharma committed Feb 16, 2024
1 parent 9a38b1c commit 6285275
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 1 addition & 3 deletions changelog.d/724.bugfix
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Fix for Issue 724(expire method fails when using DEFAULT_TIMEOUT)

https://github.com/jazzband/django-redis/issues/724
Hotfix for timeout=DEFAULT_TIMEOUT in expire and pexpire
9 changes: 3 additions & 6 deletions django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,6 @@ def expire(
version: Optional[int] = None,
client: Optional[Redis] = None,
) -> bool:
# timeout could be of type int|timedelta
# if timeout is DEFAULT_TIMEOUT or None then
# use self._backend.default_timeout(django default cache timeout)

# for some strange reason mypy complains,
# saying that timeout type is float | timedelta
if (timeout is DEFAULT_TIMEOUT) or (timeout is None):
timeout = self._backend.default_timeout # type: ignore

Expand All @@ -324,6 +318,9 @@ def pexpire(
version: Optional[int] = None,
client: Optional[Redis] = None,
) -> bool:
if (timeout is DEFAULT_TIMEOUT) or (timeout is None):
timeout = self._backend.default_timeout # type: ignore

if client is None:
client = self.get_client(write=True)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ def test_expire(self, cache: RedisCache):
def test_expire_with_default_timeout(self, cache: RedisCache):
cache.set("foo", "bar", timeout=None)
assert cache.expire("foo", DEFAULT_TIMEOUT) is True
assert cache.expire("not-existent-key", DEFAULT_TIMEOUT) is False

Check warning on line 613 in tests/test_backend.py

View check run for this annotation

Codecov / codecov/patch

tests/test_backend.py#L610-L613

Added lines #L610 - L613 were not covered by tests

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

def test_pexpire_with_default_timeout(self, cache: RedisCache):
cache.set("foo", "bar", timeout=None)
assert cache.pexpire("foo", DEFAULT_TIMEOUT) is True
assert cache.pexpire("not-existent-key", DEFAULT_TIMEOUT) is False

Check warning on line 626 in tests/test_backend.py

View check run for this annotation

Codecov / codecov/patch

tests/test_backend.py#L623-L626

Added lines #L623 - L626 were not covered by tests

def test_pexpire_at(self, cache: RedisCache):
# Test settings expiration time 1 hour ahead by datetime.
cache.set("foo", "bar", timeout=None)
Expand Down

0 comments on commit 6285275

Please sign in to comment.