Skip to content

Commit

Permalink
fix: Remove use of utcnow (psf#290)
Browse files Browse the repository at this point in the history
* Remove utcnow

This removes the use of utcnow to avoid deprecation warnings, though
ideally this would be fixed instead by switching over to using aware
datetimes instead.

* Use aware datetimes

---------

Co-authored-by: Frost Ming <mianghong@gmail.com>
  • Loading branch information
2 people authored and woodruffw committed Jul 3, 2024
1 parent af5b16a commit 17d6a09
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
7 changes: 5 additions & 2 deletions cachecontrol/caches/redis_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import division

from datetime import datetime
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Optional, Union

from cachecontrol.cache import BaseCache
Expand All @@ -26,7 +26,10 @@ def set(
if not expires:
self.conn.set(key, value)
elif isinstance(expires, datetime):
delta = expires - datetime.utcnow()
now_utc = datetime.now(timezone.utc)
if expires.tzinfo is None:
now_utc = now_utc.replace(tzinfo=None)
delta = expires - now_utc
self.conn.setex(key, int(delta.total_seconds()), value)
else:
self.conn.setex(key, expires, value)
Expand Down
6 changes: 3 additions & 3 deletions cachecontrol/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import calendar
import time
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from email.utils import formatdate, parsedate, parsedate_tz
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional

Expand All @@ -15,7 +15,7 @@


def expire_after(delta: timedelta, date: Optional[datetime] = None) -> datetime:
date = date or datetime.utcnow()
date = date or datetime.now(timezone.utc)
return date + delta


Expand Down Expand Up @@ -67,7 +67,7 @@ def update_headers(self, response: "HTTPResponse") -> Dict[str, str]:

if "expires" not in response.headers:
date = parsedate(response.headers["date"])
expires = expire_after(timedelta(days=1), date=datetime(*date[:6]))
expires = expire_after(timedelta(days=1), date=datetime(*date[:6], tzinfo=timezone.utc)) # type: ignore[misc]
headers["expires"] = datetime_to_header(expires)
headers["cache-control"] = "public"
return headers
Expand Down
9 changes: 6 additions & 3 deletions tests/test_expires_heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import calendar
import time
from datetime import datetime
from datetime import datetime, timezone
from email.utils import formatdate, parsedate
from pprint import pprint
from unittest.mock import Mock
Expand Down Expand Up @@ -157,7 +157,9 @@ def test_last_modified_is_used(self):
resp = DummyResponse(200, {"Date": self.now, "Last-Modified": self.week_ago})
modified = self.heuristic.update_headers(resp)
assert ["expires"] == list(modified.keys())
assert datetime(*parsedate(modified["expires"])[:6]) > datetime.now()

expected = datetime(*parsedate(modified["expires"])[:6], tzinfo=timezone.utc)
assert expected > datetime.now(timezone.utc)

def test_last_modified_is_not_used_when_cache_control_present(self):
resp = DummyResponse(
Expand Down Expand Up @@ -185,7 +187,8 @@ def test_last_modified_is_used_when_cache_control_public(self):
)
modified = self.heuristic.update_headers(resp)
assert ["expires"] == list(modified.keys())
assert datetime(*parsedate(modified["expires"])[:6]) > datetime.now()
expected = datetime(*parsedate(modified["expires"])[:6], tzinfo=timezone.utc)
assert expected > datetime.now(timezone.utc)

def test_warning_not_added_when_response_more_recent_than_24_hours(self):
resp = DummyResponse(200, {"Date": self.now, "Last-Modified": self.week_ago})
Expand Down
7 changes: 6 additions & 1 deletion tests/test_storage_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0

from datetime import datetime
from datetime import datetime, timezone
from unittest.mock import Mock

from cachecontrol.caches import RedisCache
Expand All @@ -17,6 +17,11 @@ def test_set_expiration_datetime(self):
self.cache.set("foo", "bar", expires=datetime(2014, 2, 2))
assert self.conn.setex.called

def test_set_expiration_datetime_aware(self):
self.cache.set("foo", "bar",
expires=datetime(2014, 2, 2, tzinfo=timezone.utc))
assert self.conn.setex.called

def test_set_expiration_int(self):
self.cache.set("foo", "bar", expires=600)
assert self.conn.setex.called

0 comments on commit 17d6a09

Please sign in to comment.