internetarchive / internetarchive/openlibrary

Fix deprecated datetime.utcnow() and datetime.utcfromtimestamp() warnings

Open
#11,855 4 comments 0 reactions 1 assignee Claimed by @RayBB View on GitHub
Lead: @RayBB Needs: Staff / Internal Priority: 3 Theme: Development Type: Bug
Dominant language
Python
Stars
6.7k
Forks
2k
Avg merge
2d 14h
Merged PRs (30d)
126

Description

### Problem

14+ deprecation warnings appear when running tests.

These are warnings now but will be removed in a future version of python.
They are also very noisy and confuse my AI when it's running tests.

More info here on the problem and the fix: https://github.com/python/cpython/issues/103857

The errors generally look something like this

```
openlibrary/tests/core/test_waitinglist.py::TestWaitingLoanGetExpiryInHours::test_expiry_in_hours_when_expiring_soon
openlibrary/tests/core/test_waitinglist.py::TestWaitingLoanGetExpiryInHours::test_expiry_in_hours_when_expired
openlibrary/tests/core/test_waitinglist.py::TestWaitingLoanGetExpiryInHours::test_expiry_in_hours_clamps_negative_to_zero
/openlibrary/openlibrary/core/waitinglist.py:79: 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).
delta = h.parse_datetime(self['expiry']) - datetime.datetime.utcnow()

openlibrary/tests/core/test_waitinglist.py::TestWaitingLoanGetExpiryInHours::test_expiry_in_hours_when_expired
/openlibrary/openlibrary/tests/core/test_waitinglist.py:202: 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).
past_time = datetime.datetime.utcnow() - datetime.timedelta(hours=5)

openlibrary/tests/core/test_waitinglist.py::TestWaitingLoanGetExpiryInHours::test_expiry_in_hours_clamps_negative_to_zero
/openlibrary/openlibrary/tests/core/test_waitinglist.py:214: 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).
five_seconds_ago = datetime.datetime.utcnow() - datetime.timedelta(seconds=5)

openlibrary/tests/core/test_waitinglist.py::TestWaitingLoanDict::test_dict_is_json_serializable
/openlibrary/openlibrary/tests/core/test_waitinglist.py:235: 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).
dt = datetime.datetime.utcnow()
```

### Reproducing the bug

1. `docker compose run --rm web make test-py` to see them (at the end)

* Expected behavior: No warnings about utc time
* Actual behavior: lots of warning

### Context

n/a

### Breakdown

Implementation Details (for maintainers)

#### Requirements Checklist
* [ ] No more warning in python tests

#### Related files

*

#### Stakeholders

*


#### Instructions for Contributors

- Please [run these commands](https://github.com/internetarchive/openlibrary/wiki/Git-Cheat-Sheet#working-on-your-branch) to ensure your repository is up to date **before** [creating a new branch](https://github.com/internetarchive/openlibrary/wiki/Git-Cheat-Sheet#making-changes-and-creating-a-pull-request) to work on this issue and **each time after** pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.