Skip to content

Commit

Permalink
Fixed UTC related deprecation warnings in Python 3.12+ (#29)
Browse files Browse the repository at this point in the history
* Fixed UTC related deprecation warnings in Python 3.12+
  • Loading branch information
zmumi authored May 4, 2024
1 parent f26c868 commit 5cd859f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.2.1
-----

* Fixed UTC related deprecation warnings in Python 3.12+

1.2.0
-----

Expand Down
2 changes: 1 addition & 1 deletion memoize/entrybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def update_timeouts(self, update_after: datetime.timedelta, expire_after: dateti
self._update_after = update_after

def build(self, key: CacheKey, value: CachedValue) -> CacheEntry:
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
return CacheEntry(created=now,
update_after=now + self._update_after,
expires_after=now + self._expires_after,
Expand Down
8 changes: 4 additions & 4 deletions memoize/serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# ignoring type error as mypy falsely reports json is already imported
import json # type: ignore
from abc import ABCMeta, abstractmethod
from datetime import datetime
from datetime import datetime, timezone

from typing import Callable, Any

Expand Down Expand Up @@ -62,9 +62,9 @@ def __init__(self, string_encoding: str = "utf-8",
def deserialize(self, data: bytes) -> CacheEntry:
as_dict = json.loads(codecs.decode(data, self.__string_encoding))
return CacheEntry(
created=datetime.utcfromtimestamp(as_dict['created']),
update_after=datetime.utcfromtimestamp(as_dict['update_after']),
expires_after=datetime.utcfromtimestamp(as_dict['expires_after']),
created=datetime.fromtimestamp(as_dict['created'], timezone.utc),
update_after=datetime.fromtimestamp(as_dict['update_after'], timezone.utc),
expires_after=datetime.fromtimestamp(as_dict['expires_after'], timezone.utc),
value=self.__reversible_repr_to_value(as_dict['value']),
)

Expand Down
2 changes: 1 addition & 1 deletion memoize/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async def wrapper(*args, **kwargs):
if current_entry is not None:
configuration_snapshot.eviction_strategy().mark_read(key)

now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)

def value_future_provider():
return _apply_timeout(configuration_snapshot.method_timeout(), method(*args, **kwargs))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def prepare_description():

setup(
name='py-memoize',
version='1.2.0',
version='1.2.1',
author='Michal Zmuda',
author_email='[email protected]',
url='https://github.com/DreamLab/memoize',
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import codecs
import json
import pickle
from datetime import datetime
from datetime import datetime, timezone
from pickle import HIGHEST_PROTOCOL, DEFAULT_PROTOCOL
from unittest.mock import Mock

Expand Down Expand Up @@ -67,8 +67,8 @@ class JsonSerDeTests(AsyncTestCase):
@gen_test
def test_should_encode_as_readable_json(self):
# given
cache_entry = CacheEntry(datetime.utcfromtimestamp(1), datetime.utcfromtimestamp(2),
datetime.utcfromtimestamp(3), "in")
cache_entry = CacheEntry(datetime.fromtimestamp(1, timezone.utc), datetime.fromtimestamp(2, timezone.utc),
datetime.fromtimestamp(3, timezone.utc), "in")
serde = JsonSerDe(string_encoding='utf-8')

# when
Expand All @@ -90,15 +90,15 @@ def test_should_decode_readable_json(self):
bytes = serde.deserialize(b'{"created":1,"update_after":2,"expires_after":3,"value":"value"}')

# then
cache_entry = CacheEntry(datetime.utcfromtimestamp(1), datetime.utcfromtimestamp(2),
datetime.utcfromtimestamp(3), "value")
cache_entry = CacheEntry(datetime.fromtimestamp(1, timezone.utc), datetime.fromtimestamp(2, timezone.utc),
datetime.fromtimestamp(3, timezone.utc), "value")
self.assertEqual(bytes, cache_entry)

@gen_test
def test_should_apply_value_transformations_on_serialization(self):
# given
cache_entry = CacheEntry(datetime.utcfromtimestamp(1), datetime.utcfromtimestamp(2),
datetime.utcfromtimestamp(3), "in")
cache_entry = CacheEntry(datetime.fromtimestamp(1, timezone.utc), datetime.fromtimestamp(2, timezone.utc),
datetime.fromtimestamp(3, timezone.utc), "in")
encode = Mock(return_value="out")
decode = Mock(return_value="in")
serde = JsonSerDe(string_encoding='utf-8', value_to_reversible_repr=encode, reversible_repr_to_value=decode)
Expand All @@ -125,8 +125,8 @@ def test_should_apply_value_transformations_on_deserialization(self):
data = serde.deserialize(b'{"created":1,"update_after":2,"expires_after":3,"value":"in"}')

# then
cache_entry = CacheEntry(datetime.utcfromtimestamp(1), datetime.utcfromtimestamp(2),
datetime.utcfromtimestamp(3), "out")
cache_entry = CacheEntry(datetime.fromtimestamp(1, timezone.utc), datetime.fromtimestamp(2, timezone.utc),
datetime.fromtimestamp(3, timezone.utc), "out")
self.assertEqual(data, cache_entry)
decode.assert_called_once_with("in")

Expand Down

0 comments on commit 5cd859f

Please sign in to comment.