Repository navigation
Replace deprecated datetime.utcnow() and utcfromtimestamp() (ruff DTZ003, DTZ004) - #14243
Merged
Merged
Conversation
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)
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. |
hornc
reviewed
Nov 1, 2026
| 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") |
Collaborator
There was a problem hiding this comment.
timestamp = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
This was referenced Nov 2, 2026
RayBB
approved these changes
Nov 5, 2026
RayBB
left a comment
Collaborator
There was a problem hiding this comment.
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.
Collaborator
|
@gauranshahuja I welcome you to try to continue the rest of the issue and complete it in another PR! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #13412
refactor: replace the deprecated
datetime.utcnow()anddatetime.utcfromtimestamp(), the first wave agreed in #13412.Technical
Replaces all 27
utcnow()calls (ruff DTZ003) and the oneutcfromtimestamp()call (DTZ004) in 16 files, and addsDTZ003andDTZ004tolint.selectinpyproject.tomlso new ones get flagged. The rest ofDTZstays commented out.Every replacement keeps returning a naive UTC datetime:
dt.datetime.now(dt.UTC).replace(tzinfo=None)anddatetime.fromtimestamp(seconds, UTC).replace(tzinfo=None). This is the form the codebase already uses inbookworm/registry.py,core/acquisitions.py,core/provider_tokens.pyandplugins/upstream/lenny.py. I kept these naive because the callers depend on that:timestampcolumns (core/edits.py,core/imports.py,coverstore/db.py,data/db.py),core/waitinglist.pywithh.parse_datetime(),plugins/upstream/addbook.pywithaccount.creation_time()), where an aware value would raiseTypeError,isoformat(), which would gain a+00:00suffix (accounts/model.pylast_login,core/edits.pycomment timestamps,plugins/openlibrary/connection.py), anddata/sitemap.pyappends a literal"Z"to it,datetime_from_utc_timestamp()feedsdatestr()inaccount/loans.htmlandadmin/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 checkandruff format --checkwith the repo config report nothing new in the changed files.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 twoutcnow()calls, and itsisoformat()has no offset.pytest . --doctest-moduleson 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. Theutcnow()deprecation warnings from our own code are gone (pytest warnings drop from 34 to 18). The one that remains comes from the vendoredinfogami/infobase/account.py.Stakeholders
@RayBB @cclauss