Skip to content

Commit

Permalink
Remove deprecated naive datetime warnings (#9895)
Browse files Browse the repository at this point in the history
* 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).
  • Loading branch information
hornc authored Sep 30, 2024
1 parent fc46984 commit 59bf298
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion openlibrary/accounts/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions openlibrary/core/bookshelves_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions openlibrary/core/processors/invalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/core/yearly_reading_goals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions openlibrary/mocks/mock_infobase.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""Simple implementation of mock infogami site to use in testing.
"""

import datetime
import glob
import itertools
import json
import re
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
Expand Down Expand Up @@ -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}
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/plugins/openlibrary/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions openlibrary/solr/updater/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
14 changes: 7 additions & 7 deletions openlibrary/tests/core/test_processors_invalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 59bf298

Please sign in to comment.