Skip to content

Replace deprecated datetime.utcnow() and utcfromtimestamp() (ruff DTZ003, DTZ004) - #14243

Merged
RayBB merged 2 commits into
internetarchive:masterfrom
gauranshahuja:13412-utc-datetimes
Nov 5, 2026
Merged

RayBB merged 2 commits into
internetarchive:masterfrom
gauranshahuja:13412-utc-datetimes

Conversation

@gauranshahuja

Copy link
Copy Markdown
Contributor

Part of #13412

refactor: replace the deprecated datetime.utcnow() and datetime.utcfromtimestamp(), the first wave agreed in #13412.

Technical

  • Replaces all 27 utcnow() calls (ruff DTZ003) and the one utcfromtimestamp() call (DTZ004) in 16 files, and adds DTZ003 and DTZ004 to lint.select in pyproject.toml so new ones get flagged. The rest of DTZ stays commented out.

  • Every replacement keeps returning a naive UTC datetime: dt.datetime.now(dt.UTC).replace(tzinfo=None) and datetime.fromtimestamp(seconds, UTC).replace(tzinfo=None). This is the form the codebase already uses in bookworm/registry.py, core/acquisitions.py, core/provider_tokens.py and plugins/upstream/lenny.py. I kept these naive because the callers depend on that:

    • the values go into timestamp columns (core/edits.py, core/imports.py, coverstore/db.py, data/db.py),
    • they are subtracted from naive datetimes (core/waitinglist.py with h.parse_datetime(), plugins/upstream/addbook.py with account.creation_time()), where an aware value would raise TypeError,
    • they are serialized with isoformat(), which would gain a +00:00 suffix (accounts/model.py last_login, core/edits.py comment timestamps, plugins/openlibrary/connection.py), and data/sitemap.py appends a literal "Z" to it,
    • datetime_from_utc_timestamp() feeds datestr() in account/loans.html and admin/loans_table.html.

    So nothing downstream changes. Moving individual call sites to aware datetimes can be a later wave, one caller at a time.

Testing

  • ruff check --select DTZ003,DTZ004 . (0.16.1, the repo's pinned version): 28 findings on master, 0 on this branch. ruff check and ruff format --check with the repo config report nothing new in the changed files.
  • Equivalence on Python 3.14.5: fromtimestamp(s, UTC).replace(tzinfo=None) == utcfromtimestamp(s) for 100,009 timestamps (fixed edge cases plus random values from 1906 to 2096); now(UTC).replace(tzinfo=None) is naive, falls between two utcnow() calls, and its isoformat() has no offset.
  • Full pytest . --doctest-modules on master and on this branch (locally, Python 3.14.5): 4559 passed on both, with the same set of failures on both, all of them local Windows issues, so the branch adds none. The utcnow() deprecation warnings from our own code are gone (pytest warnings drop from 34 to 18). The one that remains comes from the vendored infogami/infobase/account.py.

Stakeholders

@RayBB @cclauss

@openlibrary-bot

Copy link
Copy Markdown
Collaborator

Thanks for the PR, @gauranshahuja!

A reviewer must first be assigned. There are currently 138 open PRs of equal or higher priority ahead of yours.

PR triage checklist (maintainers / Pierre)
  • PR description — not empty; explains what the change does and how to verify it
  • References an issue — PR body contains a #NNN reference
    • Linked issue is triaged — has a Priority: * label (not just Needs: Triage)
    • Linked issue is assigned — has at least one assignee
  • Commit history clean — no WIP/fixup/conflict noise; commit messages are meaningful
  • CI passing — no failing check-runs
  • Test cases present — if the change touches substantive logic, test coverage exists or is explained
  • Proof of testing — PR body includes a description of what was tested, a screenshot, or a video

Note

This comment was automatically generated by Pierre, Open Library's PR first-touch assistant, on behalf of @mekarpeles. This is NOT a code review or sign-off.

Comment thread scripts/sitemaps/sitemap.py Outdated
if "siteindex.xml.gz" in filenames:
filenames.remove("siteindex.xml.gz")
timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
timestamp = datetime.now(UTC).replace(tzinfo=None).strftime("%Y-%m-%dT%H:%M:%SZ")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

timestamp = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")

@RayBB RayBB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @gauranshahuja, this is a really clean first wave. Keeping everything naive UTC and listing why each caller needs that made it easy to review.

@RayBB
RayBB merged commit 90c988c into internetarchive:master Nov 5, 2026
5 checks passed
@github-project-automation github-project-automation Bot moved this from Waiting Review/Merge from Staff to Done in Ray's Project Nov 5, 2026
@RayBB

RayBB commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

@gauranshahuja I welcome you to try to continue the rest of the issue and complete it in another PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants