googleapis / google-auth-library-python

Google Auth Python Library
https://googleapis.dev/python/google-auth/latest/
Apache License 2.0
771 stars 305 forks source link

utcnow is deprecated in python 3.12 #1399

Closed kasium closed 10 months ago

kasium commented 10 months ago

Environment details

Issue

Here is the related code

https://github.com/googleapis/google-auth-library-python/blob/d2ab3afdb567850121fec7de1d86fb5fb0fa80ed/google/auth/_helpers.py#L89-L95

arithmetic1728 commented 10 months ago

this can be fixed by replacing datetime.datetime.utcnow() with datetime.datetime.now(datetime.timezone.utc), see

https://docs.python.org/3.12/library/datetime.html#datetime.datetime.utcnow

arithmetic1728 commented 10 months ago

it looks like we cannot simply replace datetime.datetime.utcnow() with datetime.datetime.now(datetime.timezone.utc), since utcnow is offset-naive (no timezone) but now is offset-aware (has timezone). It's very common to compare the token expiry in auth lib itself as well as gcloud, if we simply switch the type, the comparison fails.

kasium commented 10 months ago

I guess you can then just remove the tzinfo afterwards like ".replace(tzinfo=None)"

arithmetic1728 commented 10 months ago

@kasium yep, that sounds like the only way. Thanks!