Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated naive datetime warnings #9895

Merged
merged 2 commits into from
Sep 30, 2024

Conversation

hornc
Copy link
Collaborator

@hornc hornc commented Sep 20, 2024

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).

These warnings were appearing ~4000 times in test results, datetime.utcnow() is deprecated in Python 3.12
For some background: https://blog.miguelgrinberg.com/post/it-s-time-for-a-change-datetime-utcnow-is-now-deprecated

This fixes the warnings caused by code in this repo. I think it doesn't introduce any new problems, but utcnow() was used in a range of places, having others review these changes would be helpful, in case there is a subtle impact from format changes I have missed.

BEFORE:

2162 passed, 9 skipped, 9 xfailed, 4891 warnings in 6.21s

AFTER:

2162 passed, 9 skipped, 9 xfailed, 20 warnings in 6.18s

Remaining utcnow() deprecation warnings are in external repos: Infogami and webpy:

openlibrary/plugins/admin/tests/test_code.py::TestRevertAllUserEdits::test_no_edits
openlibrary/plugins/admin/tests/test_code.py::TestRevertAllUserEdits::test_deletes_spam_works
openlibrary/plugins/admin/tests/test_code.py::TestRevertAllUserEdits::test_deletes_spam_works
openlibrary/plugins/admin/tests/test_code.py::TestRevertAllUserEdits::test_reverts_spam_edits
openlibrary/plugins/admin/tests/test_code.py::TestRevertAllUserEdits::test_reverts_spam_edits
openlibrary/plugins/admin/tests/test_code.py::TestRevertAllUserEdits::test_does_not_undelete
openlibrary/plugins/admin/tests/test_code.py::TestRevertAllUserEdits::test_two_spammy_editors
openlibrary/plugins/admin/tests/test_code.py::TestRevertAllUserEdits::test_two_spammy_editors
  /openlibrary/infogami/infobase/account.py:87: 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).
    now = datetime.utcnow()

openlibrary/tests/core/test_db.py::TestCheckIns::test_update_event_date
openlibrary/tests/core/test_db.py::TestYearlyReadingGoals::test_update_current_count
openlibrary/tests/core/test_db.py::TestYearlyReadingGoals::test_update_target
openlibrary/tests/core/test_imports.py::TestBatchItem::test_add_items_legacy
  /home/openlibrary/.local/lib/python3.12/site-packages/web/db.py:487: DeprecationWarning: The default timestamp converter is deprecated as of Python 3.12; see the sqlite3 documentation for suggested replacement recipes
    row = self.cursor.fetchone()

Closes #

Technical

Testing

Screenshot

Stakeholders

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).
Copy link
Collaborator

@scottbarnes scottbarnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Also, I appreciate the timezone being added to the /status page. :)

@scottbarnes scottbarnes merged commit 59bf298 into internetarchive:master Sep 30, 2024
3 checks passed
@hornc hornc deleted the fix_naive_date_deprecation branch September 30, 2024 23:21
DanielleInkster pushed a commit to DanielleInkster/openlibrary that referenced this pull request Oct 1, 2024
* 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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants