Replace deprecated datetime.utcnow() with timezone-aware equivalent#61
Open
mpasternak wants to merge 5 commits into
Open
Replace deprecated datetime.utcnow() with timezone-aware equivalent#61mpasternak wants to merge 5 commits into
mpasternak wants to merge 5 commits into
Conversation
`datetime.datetime.utcnow()` is deprecated since Python 3.12 and
scheduled for removal in a future version:
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).
`datetime_to_datestamp()` in `oaipmh/datestamp.py` asserts that the
input has `tzinfo is None`, so we convert the timezone-aware UTC
value back to naive with `.replace(tzinfo=None)` before passing it
in. The resulting `responseDate` string in the OAI-PMH response is
unchanged.
This is the only `datetime.utcnow()` call in the codebase; after
this change, pyoai no longer emits `DeprecationWarning` for it on
Python 3.12+.
7 tasks
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.
Summary
datetime.datetime.utcnow()has been deprecated since Python 3.12 and is scheduled for removal in a future Python version:The only call site in the codebase is
XMLTreeServer._outputBasicEnvelopeinsrc/oaipmh/server.py, which stamps the<responseDate>element of every OAI-PMH response.Change
datetime_to_datestamp()inoaipmh/datestamp.pyassertsdt.tzinfo is None(only accepts naive datetimes), so after obtaining a timezone-aware UTC value we striptzinfoback off. The resultingresponseDatestring in the OAI-PMH response is byte-for-byte identical to what the old code produced.Why this matters
Downstream projects running pytest with
filterwarnings = error(or-W error) fail with a 500 on every OAI-PMH verb because theDeprecationWarningis promoted to an exception during response rendering. The change is trivial and semantically transparent.Test plan
datetime_to_datestamp(datetime.now(timezone.utc).replace(microsecond=0, tzinfo=None))produces the sameYYYY-MM-DDTHH:MM:SSZformat as before, with noDeprecationWarningon Python 3.13.src/oaipmh/tests/on a supported Python version. (Note: on Python 3.12+ the existing tests fail at collection withImportError: cannot import name 'makeSuite' from 'unittest'— an unrelated issue sinceunittest.makeSuitewas removed in 3.12.)🤖 Generated with Claude Code