Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TimeSensorAsync): use DAG timezone #33406

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion airflow/sensors/time_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, *, target_time, **kwargs):
self.target_time = target_time

aware_time = timezone.coerce_datetime(
datetime.datetime.combine(datetime.datetime.today(), self.target_time)
datetime.datetime.combine(datetime.datetime.today(), self.target_time, self.dag.timezone)
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

)

self.target_datetime = timezone.convert_to_utc(aware_time)
Expand Down
13 changes: 13 additions & 0 deletions tests/sensors/test_time_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import pendulum
import pytest
import time_machine
from pendulum.tz.timezone import UTC

from airflow.exceptions import TaskDeferred
from airflow.models.dag import DAG
Expand Down Expand Up @@ -73,3 +74,15 @@ def test_target_time_aware(self):
op = TimeSensorAsync(task_id="test", target_time=aware_time)
assert hasattr(op.target_datetime.tzinfo, "offset")
assert op.target_datetime.tzinfo.offset == 0

def test_target_time_naive_dag_timezone(self):
uranusjr marked this conversation as resolved.
Show resolved Hide resolved
"""
Tests that naive target_time gets converted correctly using the DAG's timezone.
"""
with DAG(
"test_target_time_naive_dag_timezone",
start_date=pendulum.datetime(2020, 1, 1, 0, 0, tz=DEFAULT_TIMEZONE),
):
op = TimeSensorAsync(task_id="test", target_time=pendulum.time(9, 0))
assert op.target_datetime.time() == pendulum.time(1, 0)
assert op.target_datetime.tzinfo == UTC
Copy link
Member

Choose a reason for hiding this comment

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

If it fallbacks to UTC before this changes, should we test with another timezone instead?

Copy link
Contributor Author

@SapphicCode SapphicCode Sep 20, 2023

Choose a reason for hiding this comment

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

The line you're referring to here, target_datetime, is the datetime being given to DateTimeTrigger -> defer. It must always be converted to UTC.
Code - 2023-09-20 08 40 43