From 59bf29870edfbab07e89ceff328e79b11ec82fd4 Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Tue, 1 Oct 2024 05:51:51 +1300 Subject: [PATCH] Remove deprecated naive datetime warnings (#9895) * utcnow() -> now() to remove deprecated naive datetime warnings DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC). --- openlibrary/accounts/model.py | 2 +- openlibrary/core/bookshelves_events.py | 6 +++--- openlibrary/core/processors/invalidation.py | 6 +++--- openlibrary/core/yearly_reading_goals.py | 4 ++-- openlibrary/mocks/mock_infobase.py | 7 ++++--- openlibrary/plugins/openlibrary/status.py | 2 +- openlibrary/solr/updater/work.py | 4 ++-- .../tests/core/test_processors_invalidation.py | 14 +++++++------- 8 files changed, 23 insertions(+), 22 deletions(-) diff --git a/openlibrary/accounts/model.py b/openlibrary/accounts/model.py index b4b9a30f238..48cbdd080b6 100644 --- a/openlibrary/accounts/model.py +++ b/openlibrary/accounts/model.py @@ -122,7 +122,7 @@ def create_link_doc(key, username, email): """ code = generate_uuid() - now = datetime.datetime.utcnow() + now = datetime.datetime.now() expires = now + datetime.timedelta(days=14) return { diff --git a/openlibrary/core/bookshelves_events.py b/openlibrary/core/bookshelves_events.py index 59c030db166..4b07c6209b4 100644 --- a/openlibrary/core/bookshelves_events.py +++ b/openlibrary/core/bookshelves_events.py @@ -141,7 +141,7 @@ def update_event(cls, pid, edition_id=None, event_date=None, data=None): cls.TABLENAME, where='id=$id', vars={'id': pid}, - updated=datetime.utcnow(), + updated=datetime.now(), **updates, ) return 0 @@ -152,7 +152,7 @@ def update_event_date(cls, pid, event_date): where_clause = 'id=$id' where_vars = {'id': pid} - update_time = datetime.utcnow() + update_time = datetime.now() return oldb.update( cls.TABLENAME, @@ -167,7 +167,7 @@ def update_event_data(cls, pid, data): where_clause = 'id=$id' where_vars = {'id': pid} - update_time = datetime.utcnow() + update_time = datetime.now() return oldb.update( cls.TABLENAME, diff --git a/openlibrary/core/processors/invalidation.py b/openlibrary/core/processors/invalidation.py index a109b099bec..401bbee740d 100644 --- a/openlibrary/core/processors/invalidation.py +++ b/openlibrary/core/processors/invalidation.py @@ -74,7 +74,7 @@ def __init__(self, prefixes, timeout=60, cookie_name="lastupdate"): self.timeout = datetime.timedelta(0, timeout) self.cookie_name = cookie_name - self.last_poll_time = datetime.datetime.utcnow() + self.last_poll_time = datetime.datetime.now() self.last_update_time = self.last_poll_time # set expire_time slightly more than timeout @@ -106,7 +106,7 @@ def t(date): return handler() def is_timeout(self): - t = datetime.datetime.utcnow() + t = datetime.datetime.now() dt = t - self.last_poll_time return dt > self.timeout @@ -124,7 +124,7 @@ def parse_datetime(self, datestr): def reload(self): """Triggers on_new_version event for all the documents modified since last_poll_time.""" - t = datetime.datetime.utcnow() + t = datetime.datetime.now() reloaded = False keys = [] diff --git a/openlibrary/core/yearly_reading_goals.py b/openlibrary/core/yearly_reading_goals.py index 5fbdb88f1d3..21fea1e9c88 100644 --- a/openlibrary/core/yearly_reading_goals.py +++ b/openlibrary/core/yearly_reading_goals.py @@ -100,7 +100,7 @@ def update_current_count(cls, username: str, year: int, current_count: int): where=where, vars=data, current=current_count, - updated=datetime.utcnow(), + updated=datetime.now(), ) @classmethod @@ -118,7 +118,7 @@ def update_target(cls, username: str, year: int, new_target: int): where=where, vars=data, target=new_target, - updated=datetime.utcnow(), + updated=datetime.now(), ) # Delete methods: diff --git a/openlibrary/mocks/mock_infobase.py b/openlibrary/mocks/mock_infobase.py index 1720fcaa045..5110c4069b3 100644 --- a/openlibrary/mocks/mock_infobase.py +++ b/openlibrary/mocks/mock_infobase.py @@ -1,7 +1,6 @@ """Simple implementation of mock infogami site to use in testing. """ -import datetime import glob import itertools import json @@ -9,6 +8,8 @@ import pytest import web +from datetime import datetime + from infogami.infobase import client, common, account, config as infobase_config from infogami import config from openlibrary.plugins.upstream.models import Changeset @@ -79,7 +80,7 @@ def _save_doc(self, query, timestamp): def save( self, query, comment=None, action=None, data=None, timestamp=None, author=None ): - timestamp = timestamp or datetime.datetime.utcnow() + timestamp = timestamp or datetime.now() if author: author = {"key": author.key} @@ -102,7 +103,7 @@ def save( def save_many( self, query, comment=None, action=None, data=None, timestamp=None, author=None ): - timestamp = timestamp or datetime.datetime.utcnow() + timestamp = timestamp or datetime.now() docs = [self._save_doc(doc, timestamp) for doc in query] if author: diff --git a/openlibrary/plugins/openlibrary/status.py b/openlibrary/plugins/openlibrary/status.py index 5d1975ed1ed..c326ec83714 100644 --- a/openlibrary/plugins/openlibrary/status.py +++ b/openlibrary/plugins/openlibrary/status.py @@ -110,7 +110,7 @@ def setup(): "Software version": get_software_version(), "Python version": sys.version.split()[0], "Host": host, - "Start time": datetime.datetime.utcnow(), + "Start time": datetime.datetime.now(datetime.UTC), } feature_flags = get_features_enabled() diff --git a/openlibrary/solr/updater/work.py b/openlibrary/solr/updater/work.py index 262e2040b62..3ce53547fcb 100644 --- a/openlibrary/solr/updater/work.py +++ b/openlibrary/solr/updater/work.py @@ -233,9 +233,9 @@ def datetimestr_to_int(datestr): try: t = h.parse_datetime(datestr) except (TypeError, ValueError): - t = datetime.datetime.utcnow() + t = datetime.datetime.now() else: - t = datetime.datetime.utcnow() + t = datetime.datetime.now() return int(time.mktime(t.timetuple())) diff --git a/openlibrary/tests/core/test_processors_invalidation.py b/openlibrary/tests/core/test_processors_invalidation.py index 442ef7d6ae4..1cc1915edc5 100644 --- a/openlibrary/tests/core/test_processors_invalidation.py +++ b/openlibrary/tests/core/test_processors_invalidation.py @@ -17,12 +17,12 @@ def on_new_version(self, doc): class MockDatetime: - """Class to mock datetime.datetime to overwrite utcnow method.""" + """Class to mock datetime.datetime to overwrite now() method.""" - def __init__(self, utcnow): - self._now = utcnow + def __init__(self, mock_now): + self._now = mock_now - def utcnow(self): + def now(self): return self._now @@ -73,7 +73,7 @@ def test_reload(self, monkeypatch): def test_reload_on_timeout(self, monkeypatch): # create the processor at 60 seconds past in time - mock_now = datetime.datetime.utcnow() - datetime.timedelta(seconds=60) + mock_now = datetime.datetime.now() - datetime.timedelta(seconds=60) monkeypatch.setattr(datetime, "datetime", MockDatetime(mock_now)) p = invalidation.InvalidationProcessor(prefixes=['/templates'], timeout=60) @@ -99,7 +99,7 @@ def test_reload_on_timeout(self, monkeypatch): def test_is_timeout(self, monkeypatch): # create the processor at 60 seconds past in time - mock_now = datetime.datetime.utcnow() - datetime.timedelta(seconds=60) + mock_now = datetime.datetime.now() - datetime.timedelta(seconds=60) monkeypatch.setattr(datetime, "datetime", MockDatetime(mock_now)) p = invalidation.InvalidationProcessor(prefixes=['/templates'], timeout=60) @@ -135,7 +135,7 @@ def test_reload_on_cookie(self, monkeypatch): assert self.hook.call_count == 0 web.ctx.env['HTTP_COOKIE'] = ( - "invalidation_cookie=" + datetime.datetime.utcnow().isoformat() + "invalidation_cookie=" + datetime.datetime.now().isoformat() ) # Clear parsed cookie cache to force our new value to be parsed if "_parsed_cookies" in web.ctx: