botocore icon indicating copy to clipboard operation
botocore copied to clipboard

Resolve Python 3.12 `.utcnow()` `DeprecationWarning`

Open kurtmckee opened this issue 1 year ago • 5 comments

This PR resolves the Python 3.12 DeprecationWarning thrown when botocore calls datetime.datetime.utcnow().

@nateprewitt, this PR differs from other attempts posted to this repo by ensuring datetime instances are still timezone-unaware:

import datetime

# Current form: timezone-unaware, raises DeprecationWarnings in >=py3.12
datetime.datetime.utcnow()

# Common transformation -- makes the instance timezone-aware!
datetime.datetime.now(datetime.timezone.utc)

# This PR's approach -- ensures instance is still timezone-unaware
datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)

I ran this through CI in my own repo and it passes cleanly.

Fixes boto/boto3#3889.

kurtmckee avatar Aug 16 '24 18:08 kurtmckee

It would be good to get this merged soon, it's been something tracked over on the boto3 issues for almost a year

StevenMapes avatar Sep 26 '24 09:09 StevenMapes

@SamRemis, @nateprewitt, @jonathan343 -- Friendly request to review this PR when you have an opportunity.

kurtmckee avatar Sep 26 '24 11:09 kurtmckee

@kurtmckee thank you so much for looking into this. This DeprecationWarning transitively via boto has forced my terminal to scrolls 1000s of kilometers over the past year.

Since this PR keeps the timezone-less functionality, consider putting datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) into a utility method that will make it harder to get it wrong and make it easier to find all the places where timezone-less datetimes will need to be remediated. naive_utcnow() suggested as the name based on the blogpost about this deprecation.

aleyan avatar Sep 26 '24 16:09 aleyan

There are now at least four PRs trying to adress this issue and none of these have been merged.

Could any of the PRs #3089, #3093, #3145 or #3239 be merged to address this warning before it becomes are hard error?

glaubitz avatar Sep 30 '24 06:09 glaubitz

ALL -- Please do not add junk comments.

They contribute nothing and pollute actual discussion if and when it happens.

kurtmckee avatar Sep 30 '24 11:09 kurtmckee

@nateprewitt @SamRemis Hello! Please review this PR when you can.

It resolves Python 3.12 utcnow DeprecationWarnings without introducing serialization problems caused by timezone-aware datetime instances.

kurtmckee avatar Jul 07 '25 13:07 kurtmckee

Thanks @kurtmckee! We're aware of the PR and are looking to have a more comprehensive set of changes that build on this PR in the near future. This change will need to span several projects so it's taken a bit longer to allocate time for the work/review. We'll let you know when we're closer to a release.

nateprewitt avatar Jul 07 '25 16:07 nateprewitt

You're welcome! :partying_face:

kurtmckee avatar Aug 01 '25 20:08 kurtmckee