Repository navigation
[Python] Replace deprecated utcnow and utcfromtimestamp calls - #41740
Anurag-S1ngh wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR cleanly replaces deprecated datetime.datetime.utcnow() and datetime.datetime.utcfromtimestamp() calls across six files in the Python SDK with timezone-aware equivalents (datetime.fromtimestamp(..., timezone.utc), datetime.now(timezone.utc)) or explicit epoch literals (datetime(1970, 1, 1)).
🔍 General Feedback
- Naive vs. timezone-aware semantics are carefully preserved where downstream formatting or comparisons depend on naive UTC
datetimeobjects (e.g.,io_test.pyandhourly_team_score.py). - Removing the redundant
dateutil.tzimport inpcoll_visualization.pyin favor ofdatetime.timezone.utcis a nice cleanup. - The changes are well-scoped and ready to merge.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
datetime.utcnow() and datetime.utcfromtimestamp() emit DeprecationWarning on Python 3.12 and are scheduled for removal. Replace them with timezone aware equivalents, keeping naive values where existing comparisons and formatted output expect them. Addresses apache#18303
a500ede to
e1a8989
Compare
|
Assigning reviewers: R: @claudevdm for label python. This pull request likely touches a core component ("core" label). Please review with scrutiny. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Replaces deprecated datetime APIs in the Python SDK that emit
DeprecationWarningon Python 3.12.datetime.datetime.utcnow()anddatetime.datetime.utcfromtimestamp()are scheduled for removal, and Beam supports Python 3.11 through 3.14.This is a first slice of #18303, limited to the datetime deprecations so it stays easy to review.
Changes
apache_beam/dataframe/io_test.py: build expected window timestamps withdatetime.fromtimestamp(..., timezone.utc).apache_beam/io/gcp/bigquery_tools.py: convert BigQuery TIMESTAMP cells with a UTC-awarefromtimestamp.apache_beam/io/gcp/datastore/v1new/types_test.py: usedatetime.now(timezone.utc)in the entity conversion test.apache_beam/examples/complete/game/hourly_team_score.py: use the epoch date literal instead ofutcfromtimestamp(0).apache_beam/runners/interactive/display/pcoll_visualization.py: use an awarefromtimestampand remove the unuseddateutil.tzimport.apache_beam/typehints/schemas.py: use the epoch date literal inJdbcTimeType.Naive UTC values are kept where existing comparisons or formatted output depend on them.
Tests
python -m pytest apache_beam/dataframe/io_test.py -k test_windowed_writepython -m pytest apache_beam/io/gcp/datastore/v1new/types_test.pypython -m pytest apache_beam/typehints/schemas_test.pyyapf --diffon all touched filesaddresses #18303