Skip to content

Commit c9b2a7f

Browse files
committed
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
1 parent f9a6ba1 commit c9b2a7f

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

invenio_requests/records/systemfields/expired_state.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# -*- coding: utf-8 -*-
22
#
33
# Copyright (C) 2021 - 2022 TU Wien.
4+
# Copyright (C) 2025 Graz University of Technology.
45
#
56
# Invenio-Requests is free software; you can redistribute it and/or modify
67
# it under the terms of the MIT License; see LICENSE file for more details.
78

89
"""Systemfield for calculating the ``is_expired`` property of a request."""
910

1011

11-
from datetime import datetime
12+
from datetime import datetime, timezone
1213

13-
import pytz
14+
import arrow
1415
from invenio_records_resources.records.systemfields.calculated import CalculatedField
1516

1617

@@ -29,10 +30,7 @@ def calculate(self, record):
2930
if expires_at is None:
3031
return False
3132

32-
# comparing timezone-aware and naive datetimes results in an error
33-
# https://docs.python.org/3/library/datetime.html#determining-if-an-object-is-aware-or-naive # noqa
34-
now = datetime.utcnow()
35-
if expires_at.tzinfo and expires_at.tzinfo.utcoffset(expires_at) is not None:
36-
now = now.replace(tzinfo=pytz.utc)
33+
expires_at = arrow.get(expires_at, tzinfo=timezone.utc).datetime
34+
now = datetime.now(timezone.utc)
3735

3836
return expires_at < now

invenio_requests/tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# -*- coding: utf-8 -*-
22
#
33
# This file is part of Invenio.
4-
# Copyright (C) 2022 Graz University of Technology.
4+
# Copyright (C) 2022-2025 Graz University of Technology.
55
#
66
# Invenio is free software; you can redistribute it and/or modify it
77
# under the terms of the MIT License; see LICENSE file for more details.
88

99
"""Celery tasks for requests."""
1010

11-
from datetime import datetime
11+
from datetime import datetime, timezone
1212

1313
from celery import shared_task
1414
from flask import current_app
@@ -25,7 +25,7 @@
2525
def check_expired_requests():
2626
"""Retrieve expired requests and perform expired action."""
2727
service = current_requests_service
28-
now = datetime.utcnow().isoformat()
28+
now = datetime.now(timezone.utc).isoformat()
2929

3030
# using scan to get all requests
3131
requests_list = service.scan(

tests/records/systemfields/test_calculated_systemfield.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# -*- coding: utf-8 -*-
22
#
33
# Copyright (C) 2021 TU Wien.
4+
# Copyright (C) 2025 Graz University of Technology.
45
#
56
# Invenio-Requests is free software; you can redistribute it and/or modify it
67
# under the terms of the MIT License; see LICENSE file for more details.
78

89
"""Test the calculated systemfields."""
910

10-
from datetime import datetime, timedelta
11+
from datetime import datetime, timedelta, timezone
1112

1213
import pytest
1314

@@ -16,7 +17,7 @@
1617

1718
def test_expired_systemfield(example_request):
1819
"""Test if the expired system field works as intended."""
19-
now = datetime.utcnow()
20+
now = datetime.now(timezone.utc)
2021
example_request.expires_at = None
2122
example_request.commit()
2223

tests/services/requests/test_requests_tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2022 Graz University of Technology.
3+
# Copyright (C) 2022-2025 Graz University of Technology.
44
#
55
# Invenio-Requests is free software; you can redistribute it and/or
66
# modify it under the terms of the MIT License; see LICENSE file for more
77
# details.
88

99
"""Tasks tests."""
1010

11-
from datetime import datetime, timedelta
11+
from datetime import datetime, timedelta, timezone
1212

1313
from invenio_access.permissions import system_identity
1414
from invenio_search.engine import dsl
@@ -21,7 +21,7 @@ def test_check_expired_requests(
2121
app, identity_simple, create_request, submit_request, requests_service
2222
):
2323
"""Test if the expired system field works as intended."""
24-
now = datetime.utcnow()
24+
now = datetime.now(timezone.utc)
2525

2626
# created only should not be picked up
2727
created_request = create_request(

0 commit comments

Comments
 (0)