Skip to content

[Python] Replace deprecated utcnow and utcfromtimestamp calls - #41740

Open
Anurag-S1ngh wants to merge 1 commit into
apache:masterfrom
Anurag-S1ngh:fix/18303-deprecated-datetime-usage
Open

Anurag-S1ngh wants to merge 1 commit into
apache:masterfrom
Anurag-S1ngh:fix/18303-deprecated-datetime-usage

Conversation

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

Replaces deprecated datetime APIs in the Python SDK that emit DeprecationWarning on Python 3.12. datetime.datetime.utcnow() and datetime.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 with datetime.fromtimestamp(..., timezone.utc).
  • apache_beam/io/gcp/bigquery_tools.py: convert BigQuery TIMESTAMP cells with a UTC-aware fromtimestamp.
  • apache_beam/io/gcp/datastore/v1new/types_test.py: use datetime.now(timezone.utc) in the entity conversion test.
  • apache_beam/examples/complete/game/hourly_team_score.py: use the epoch date literal instead of utcfromtimestamp(0).
  • apache_beam/runners/interactive/display/pcoll_visualization.py: use an aware fromtimestamp and remove the unused dateutil.tz import.
  • apache_beam/typehints/schemas.py: use the epoch date literal in JdbcTimeType.

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_write
  • python -m pytest apache_beam/io/gcp/datastore/v1new/types_test.py
  • python -m pytest apache_beam/typehints/schemas_test.py
  • yapf --diff on all touched files

addresses #18303

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 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 datetime objects (e.g., io_test.py and hourly_team_score.py).
  • Removing the redundant dateutil.tz import in pcoll_visualization.py in favor of datetime.timezone.utc is 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
@Anurag-S1ngh
Anurag-S1ngh force-pushed the fix/18303-deprecated-datetime-usage branch from a500ede to e1a8989 Compare November 29, 2026 02:49
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @claudevdm for label python.
Backup reviewer: shunping (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

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 assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant