From 0844b557c1a17e7169e9129d4d5769b6ee854ac4 Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Tue, 20 Jan 2026 14:40:05 +0100 Subject: [PATCH] fix(chore): DeprecationWarning stdlib * 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). * the decision was made to move to utc aware timestamps --- .../2f62e6442a08_change_datetime_types.py | 34 +++++++++++++++++++ invenio_github/models.py | 6 ++-- invenio_github/tasks.py | 3 +- invenio_github/utils.py | 8 ++--- 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 invenio_github/alembic/2f62e6442a08_change_datetime_types.py diff --git a/invenio_github/alembic/2f62e6442a08_change_datetime_types.py b/invenio_github/alembic/2f62e6442a08_change_datetime_types.py new file mode 100644 index 00000000..ec8122fb --- /dev/null +++ b/invenio_github/alembic/2f62e6442a08_change_datetime_types.py @@ -0,0 +1,34 @@ +# +# This file is part of Invenio. +# Copyright (C) 2016-2018 CERN. +# Copyright (C) 2026 Graz University of Technology. +# +# Invenio is free software; you can redistribute it and/or modify it +# under the terms of the MIT License; see LICENSE file for more details. + +"""Change expires_at type to utc aware datetime.""" + +from invenio_db.utils import ( + update_table_columns_column_type_to_datetime, + update_table_columns_column_type_to_utc_datetime, +) + +# revision identifiers, used by Alembic. +revision = "2f62e6442a08" +down_revision = "b0eaee37b545" +branch_labels = () +depends_on = None + + +def upgrade(): + """Upgrade database.""" + for table_name in ["github_repositories", "github_releases"]: + update_table_columns_column_type_to_utc_datetime(table_name, "created") + update_table_columns_column_type_to_utc_datetime(table_name, "updated") + + +def downgrade(): + """Downgrade database.""" + for table_name in ["github_repositories", "github_releases"]: + update_table_columns_column_type_to_datetime(table_name, "created") + update_table_columns_column_type_to_datetime(table_name, "updated") diff --git a/invenio_github/models.py b/invenio_github/models.py index 78ed0f96..9e9a1451 100644 --- a/invenio_github/models.py +++ b/invenio_github/models.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2023 CERN. +# Copyright (C) 2026 Graz University of Technology. # # Invenio is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License as @@ -32,7 +33,6 @@ from invenio_i18n import lazy_gettext as _ from invenio_webhooks.models import Event from sqlalchemy.dialects import postgresql -from sqlalchemy_utils.models import Timestamp from sqlalchemy_utils.types import ChoiceType, JSONType, UUIDType RELEASE_STATUS_TITLES = { @@ -107,7 +107,7 @@ def color(self): return RELEASE_STATUS_COLOR[self.name] -class Repository(db.Model, Timestamp): +class Repository(db.Model, db.Timestamp): """Information about a GitHub repository.""" __tablename__ = "github_repositories" @@ -203,7 +203,7 @@ def __repr__(self): return "".format(self=self) -class Release(db.Model, Timestamp): +class Release(db.Model, db.Timestamp): """Information about a GitHub release.""" __tablename__ = "github_releases" diff --git a/invenio_github/tasks.py b/invenio_github/tasks.py index 36755e7e..0dc50037 100644 --- a/invenio_github/tasks.py +++ b/invenio_github/tasks.py @@ -3,6 +3,7 @@ # This file is part of Invenio. # Copyright (C) 2023 CERN. # Copyright (C) 2024 KTH Royal Institute of Technology. +# Copyright (C) 2026 Graz University of Technology. # # Invenio is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -154,7 +155,7 @@ def refresh_accounts(expiration_threshold=None): :param expiration_threshold: Dictionary containing timedelta parameters referring to the maximum inactivity time. """ - expiration_date = datetime.datetime.utcnow() - datetime.timedelta( + expiration_date = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta( **(expiration_threshold or {"days": 6 * 30}) ) diff --git a/invenio_github/utils.py b/invenio_github/utils.py index 64034ac3..49a609b0 100644 --- a/invenio_github/utils.py +++ b/invenio_github/utils.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2023 CERN. +# Copyright (C) 2026 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -19,17 +20,16 @@ """Various utility functions.""" -from datetime import datetime +from datetime import datetime, timezone import dateutil.parser -import pytz import six from werkzeug.utils import import_string def utcnow(): """UTC timestamp (with timezone).""" - return datetime.now(tz=pytz.utc) + return datetime.now(tz=timezone.utc) def iso_utcnow(): @@ -41,7 +41,7 @@ def parse_timestamp(x): """Parse ISO8601 formatted timestamp.""" dt = dateutil.parser.parse(x) if dt.tzinfo is None: - dt = dt.replace(tzinfo=pytz.utc) + dt = dt.replace(tzinfo=timezone.utc) return dt