housekeep: replace deprecated datetime functions

tests show warnings for deprecated utc functions with python 3.12

Signed-off-by: moson <moson@archlinux.org>
This commit is contained in:
moson 2024-04-25 18:24:16 +02:00
parent ffddf63975
commit afb7af3e27
No known key found for this signature in database
GPG key ID: 4A4760AB4EE15296
9 changed files with 21 additions and 21 deletions

View file

@ -1,4 +1,4 @@
from datetime import datetime from datetime import UTC, datetime
class Benchmark: class Benchmark:
@ -7,7 +7,7 @@ class Benchmark:
def _timestamp(self) -> float: def _timestamp(self) -> float:
"""Generate a timestamp.""" """Generate a timestamp."""
return float(datetime.utcnow().timestamp()) return float(datetime.now(UTC).timestamp())
def start(self) -> int: def start(self) -> int:
"""Start a benchmark.""" """Start a benchmark."""

View file

@ -1,6 +1,6 @@
import copy import copy
import math import math
from datetime import datetime from datetime import UTC, datetime
from typing import Any, Union from typing import Any, Union
from urllib.parse import quote_plus, urlencode from urllib.parse import quote_plus, urlencode
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
@ -94,7 +94,7 @@ def tn(context: dict[str, Any], count: int, singular: str, plural: str) -> str:
@register_filter("dt") @register_filter("dt")
def timestamp_to_datetime(timestamp: int): def timestamp_to_datetime(timestamp: int):
return datetime.utcfromtimestamp(int(timestamp)) return datetime.fromtimestamp(timestamp, UTC)
@register_filter("as_timezone") @register_filter("as_timezone")

View file

@ -3,7 +3,7 @@ import importlib
import os import os
import sys import sys
import traceback import traceback
from datetime import datetime from datetime import UTC, datetime
import orjson import orjson
import pygit2 import pygit2
@ -60,7 +60,7 @@ def update_repository(repo: pygit2.Repository):
except pygit2.GitError: except pygit2.GitError:
base = [] base = []
utcnow = datetime.utcnow() utcnow = datetime.now(UTC)
author = pygit2.Signature( author = pygit2.Signature(
config.get("git-archive", "author"), config.get("git-archive", "author"),
config.get("git-archive", "author-email"), config.get("git-archive", "author-email"),

View file

@ -1,6 +1,6 @@
import zoneinfo import zoneinfo
from collections import OrderedDict from collections import OrderedDict
from datetime import datetime from datetime import UTC, datetime
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
from fastapi import Request from fastapi import Request
@ -89,4 +89,4 @@ def utcnow() -> int:
:return: Current UTC timestamp :return: Current UTC timestamp
""" """
return int(datetime.utcnow().timestamp()) return int(datetime.now(UTC).timestamp())

View file

@ -15,7 +15,7 @@ import os
import random import random
import sys import sys
import time import time
from datetime import datetime from datetime import UTC, datetime
import bcrypt import bcrypt
@ -263,7 +263,7 @@ for p in list(seen_pkgs.keys()):
out.write(s) out.write(s)
# Cast votes # Cast votes
utcnow = int(datetime.utcnow().timestamp()) utcnow = int(datetime.now(UTC).timestamp())
track_votes = {} track_votes = {}
log.debug("Casting votes for packages.") log.debug("Casting votes for packages.")

View file

@ -1,6 +1,6 @@
import re import re
import tempfile import tempfile
from datetime import datetime from datetime import UTC, datetime
from http import HTTPStatus from http import HTTPStatus
from logging import DEBUG from logging import DEBUG
from subprocess import Popen from subprocess import Popen
@ -394,7 +394,7 @@ def test_post_register_error_ip_banned(client: TestClient):
# 'testclient' is our fallback value in case request.client is None # 'testclient' is our fallback value in case request.client is None
# which is the case for TestClient # which is the case for TestClient
with db.begin(): with db.begin():
create(Ban, IPAddress="testclient", BanTS=datetime.utcnow()) create(Ban, IPAddress="testclient", BanTS=datetime.now(UTC))
with client as request: with client as request:
response = post_register(request) response = post_register(request)

View file

@ -1,5 +1,5 @@
import warnings import warnings
from datetime import datetime, timedelta from datetime import UTC, datetime, timedelta
import pytest import pytest
from sqlalchemy import exc as sa_exc from sqlalchemy import exc as sa_exc
@ -17,7 +17,7 @@ def setup(db_test):
@pytest.fixture @pytest.fixture
def ban() -> Ban: def ban() -> Ban:
ts = datetime.utcnow() + timedelta(seconds=30) ts = datetime.now(UTC) + timedelta(seconds=30)
with db.begin(): with db.begin():
ban = create(Ban, IPAddress="127.0.0.1", BanTS=ts) ban = create(Ban, IPAddress="127.0.0.1", BanTS=ts)
yield ban yield ban
@ -30,7 +30,7 @@ def test_ban(ban: Ban):
def test_invalid_ban(): def test_invalid_ban():
with pytest.raises(sa_exc.IntegrityError): with pytest.raises(sa_exc.IntegrityError):
bad_ban = Ban(BanTS=datetime.utcnow()) bad_ban = Ban(BanTS=datetime.now(UTC))
# We're adding a ban with no primary key; this causes an # We're adding a ban with no primary key; this causes an
# SQLAlchemy warnings when committing to the DB. # SQLAlchemy warnings when committing to the DB.

View file

@ -1,4 +1,4 @@
from datetime import datetime from datetime import UTC, datetime
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
import pytest import pytest
@ -8,7 +8,7 @@ from aurweb import filters, time
def test_timestamp_to_datetime(): def test_timestamp_to_datetime():
ts = time.utcnow() ts = time.utcnow()
dt = datetime.utcfromtimestamp(int(ts)) dt = datetime.fromtimestamp(ts, UTC)
assert filters.timestamp_to_datetime(ts) == dt assert filters.timestamp_to_datetime(ts) == dt

View file

@ -1,6 +1,6 @@
import hashlib import hashlib
import json import json
from datetime import datetime, timedelta from datetime import UTC, datetime, timedelta
import bcrypt import bcrypt
import pytest import pytest
@ -135,7 +135,7 @@ def test_user_login_twice(user: User):
def test_user_login_banned(user: User): def test_user_login_banned(user: User):
# Add ban for the next 30 seconds. # Add ban for the next 30 seconds.
banned_timestamp = datetime.utcnow() + timedelta(seconds=30) banned_timestamp = datetime.now(UTC) + timedelta(seconds=30)
with db.begin(): with db.begin():
db.create(Ban, IPAddress="127.0.0.1", BanTS=banned_timestamp) db.create(Ban, IPAddress="127.0.0.1", BanTS=banned_timestamp)
@ -170,7 +170,7 @@ def test_user_login_with_outdated_sid(user: User):
Session, Session,
UsersID=user.ID, UsersID=user.ID,
SessionID="stub", SessionID="stub",
LastUpdateTS=datetime.utcnow().timestamp() - 5, LastUpdateTS=datetime.now(UTC).timestamp() - 5,
) )
sid = user.login(Request(), "testPassword") sid = user.login(Request(), "testPassword")
assert sid and user.is_authenticated() assert sid and user.is_authenticated()
@ -279,7 +279,7 @@ def test_user_is_developer(user: User):
def test_user_voted_for(user: User, package: Package): def test_user_voted_for(user: User, package: Package):
pkgbase = package.PackageBase pkgbase = package.PackageBase
now = int(datetime.utcnow().timestamp()) now = int(datetime.now(UTC).timestamp())
with db.begin(): with db.begin():
db.create(PackageVote, PackageBase=pkgbase, User=user, VoteTS=now) db.create(PackageVote, PackageBase=pkgbase, User=user, VoteTS=now)
assert user.voted_for(package) assert user.voted_for(package)