Skip to content

Replace deprecated datetime.datetime.utcnow - #480

Closed
kajinamit wants to merge 1 commit into
Pylons:mainfrom
kajinamit:utcnow
Closed

Replace deprecated datetime.datetime.utcnow#480
kajinamit wants to merge 1 commit into
Pylons:mainfrom
kajinamit:utcnow

Conversation

@kajinamit

@kajinamit kajinamit commented Jun 9, 2025

Copy link
Copy Markdown
Contributor

The utcnow function was deprecated in Python 3.12[1].

Note that all datetime instances are kept non-timezone-aware to keep backword-compatibility.

[1] https://docs.python.org/3.13/library/datetime.html#datetime.datetime.utcnow

@kajinamit
kajinamit force-pushed the utcnow branch 3 times, most recently from 21641ad to 1e799b5 Compare June 9, 2025 14:48
@piotr-dobrogost

Copy link
Copy Markdown

Related issue #473

@kajinamit kajinamit closed this Feb 8, 2026
@kajinamit kajinamit reopened this Feb 8, 2026
@kajinamit
kajinamit force-pushed the utcnow branch 2 times, most recently from 71a2f91 to 46d284e Compare February 8, 2026 13:06
@miketheman

Copy link
Copy Markdown
Contributor

Duplicate of #475 and does a lot more - looks like you ran an auto-formatter or something changed a lot more lines than is relevant?

@kajinamit

Copy link
Copy Markdown
Contributor Author

Duplicate of #475 and does a lot more - looks like you ran an auto-formatter or something changed a lot more lines than is relevant?

That was due to the base commit to fix the lint check error. I removed that commit, because it was merged separately.

Comment thread src/webob/datetime_utils.py
The utcnow function was deprecated in Python 3.12[1].

Note that all datetime instances are kept non-timezone-aware to keep
backword-compatibility.

[1] https://docs.python.org/3.13/library/datetime.html#datetime.datetime.utcnow
@digitalresistor

Copy link
Copy Markdown
Member

Thank you for this, and apologies for how long it sat.

There were three overlapping PRs open against #473 and #430 (this one, #475 and #491), all rewriting the same lines, so none could be merged alongside the others. I've combined them into #496, where your commit is preserved with you as the author.

Your approach is the base of that branch: the webob.datetime_utils.utcnow() helper returning a naive UTC datetime, which is what @mmerickel favoured in his review of #475 precisely because it keeps the public API surface unchanged. Nothing WebOb exposes changes type, which leaves the tz-aware migration discussed in #473 as a separate piece of work.

Your commit is unmodified. It also ended up resolving #430: _now now points at your helper, which fixes the naive-local-time bug in both serialize_date() and parse_date_delta() at once.

Closing here since the work continues in #496 — it's open for review, not merged yet.

digitalresistor added a commit that referenced this pull request Aug 3, 2026
Combining #475 and #480 leaves one behavior change worth guarding: 475's
unconditional v.astimezone(timezone.utc) treats a *naive* datetime as local
time, shifting it by the machine's UTC offset. WebOb has always treated naive
datetimes as UTC, and CI runs in UTC so the change is invisible there.

Gate the conversion on v.tzinfo, keeping 475's fix for aware datetimes while
leaving naive ones alone, and cover the naive/aware/date paths plus the new
utcnow() helper.
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.

5 participants