Skip to content

Conversation

@andriawang6
Copy link

@andriawang6 andriawang6 commented Jul 30, 2025

addresses #1781

  • Moves away from using utcfromtimestamp() in the datetime library due to deprecation.
  • Adds a new helper function to manage internal handling of timezone aware versus timezone naive timestamps.
DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).

@andriawang6 andriawang6 force-pushed the replace-utcfromtimestamp branch from 687ac1a to 250f015 Compare July 30, 2025 00:58
@andriawang6 andriawang6 marked this pull request as ready for review July 30, 2025 01:06
@andriawang6 andriawang6 requested review from a team as code owners July 30, 2025 01:06
@gkevinzheng gkevinzheng requested a review from a team as a code owner December 17, 2025 19:26
gkevinzheng
gkevinzheng previously approved these changes Dec 17, 2025
@gkevinzheng gkevinzheng changed the title replace deprecated utcfromtimestamp fix: replace deprecated utcfromtimestamp Dec 17, 2025
parthea
parthea previously approved these changes Dec 17, 2025
@parthea parthea added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Dec 17, 2025
@yoshi-kokoro yoshi-kokoro removed kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Dec 17, 2025
@gkevinzheng gkevinzheng enabled auto-merge (squash) December 17, 2025 19:35
@parthea parthea added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Dec 17, 2025
@parthea parthea disabled auto-merge December 17, 2025 20:50
@parthea parthea self-assigned this Dec 17, 2025
@gkevinzheng gkevinzheng dismissed their stale review December 18, 2025 15:34

Failing tests due to not stripping tzinfo

@gkevinzheng
Copy link
Contributor

gkevinzheng commented Dec 18, 2025

Hi @andriawang6,

I don't think you can simply replace utcfromtimestamp with fromtimestamp(tz=datetime.timezone.utc) since utcfromtimestamp is not offset aware, whereas fromtimestamp is. This causes issues with comparing timestamps. We've resolved a similar issue in the past (see this discussion) by adding a .replace(tzinfo=None) after creating the timestamp object with fromtimestamp(tz=datetime.timezone.utc). You can add it to google/auth/_helpers.py like we did the function definition for utcnow

Also see this PR: #1413

@chalmerlowe chalmerlowe added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 29, 2025
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 29, 2025
@chalmerlowe chalmerlowe added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 29, 2025
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 29, 2025
@Linchin Linchin self-requested a review December 30, 2025 00:24
Copy link
Contributor

@Linchin Linchin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add unit test for utcfromtimestamp() in tests/test__helpers.py.

@chalmerlowe chalmerlowe added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 30, 2025
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 30, 2025
@chalmerlowe chalmerlowe added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 30, 2025
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 30, 2025
@chalmerlowe chalmerlowe added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 30, 2025
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 30, 2025
@chalmerlowe chalmerlowe removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Jan 5, 2026
@chalmerlowe
Copy link
Contributor

Removed the Do-Not-Merge label after discussion with @parthea. Further work on this PR appears to have resolved the issue in question as identified by Kevin: #1799 (comment)

@chalmerlowe
Copy link
Contributor

chalmerlowe commented Jan 5, 2026

I think we need to add unit test for utcfromtimestamp() in tests/test__helpers.py.
[From this comment](#1799 (review)

@Linchin
Added unit tests.

Copy link
Contributor

@chalmerlowe chalmerlowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@parthea parthea added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Jan 5, 2026
@yoshi-kokoro yoshi-kokoro removed kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Jan 5, 2026
assert token == id_token
# JWT does not store microseconds
now = now.replace(microsecond=0)
now = now.replace(microsecond=0).replace(tzinfo=None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up on offline discussion, tests pass regardless of the changes in tests_async/oauth2/test__client_async.py. Revert this file as it appears to be noise

@mock.patch(
"google.auth._helpers.utcnow",
return_value=datetime.datetime.utcfromtimestamp(0),
return_value=datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use _helpers.utcfromtimestamp . Applies throughout

@parthea parthea marked this pull request as draft January 5, 2026 17:42
@parthea
Copy link
Contributor

parthea commented Jan 5, 2026

Switching to draft while review comments are being addressed

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.

7 participants