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

Better typing for BaseOperator defer #39742

Merged
merged 1 commit into from
May 21, 2024
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
3 changes: 2 additions & 1 deletion airflow/models/baseoperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
Callable,
Collection,
Iterable,
NoReturn,
Sequence,
TypeVar,
Union,
Expand Down Expand Up @@ -1706,7 +1707,7 @@ def defer(
method_name: str,
kwargs: dict[str, Any] | None = None,
timeout: timedelta | None = None,
):
) -> NoReturn:
"""
Mark this Operator "deferred", suspending its execution until the provided trigger fires an event.

Expand Down
2 changes: 1 addition & 1 deletion airflow/providers/databricks/operators/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ def monitor_databricks_job(self) -> None:
run_state = RunState(**run["state"])
self.log.info("Current state of the job: %s", run_state.life_cycle_state)
if self.deferrable and not run_state.is_terminal:
return self.defer(
self.defer(
trigger=DatabricksExecutionTrigger(
run_id=self.databricks_run_id,
databricks_conn_id=self.databricks_conn_id,
Expand Down
6 changes: 3 additions & 3 deletions airflow/sensors/date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from __future__ import annotations

import datetime
from typing import TYPE_CHECKING, Sequence
from typing import TYPE_CHECKING, NoReturn, Sequence

from airflow.sensors.base import BaseSensorOperator
from airflow.triggers.temporal import DateTimeTrigger
Expand Down Expand Up @@ -90,13 +90,13 @@ class DateTimeSensorAsync(DateTimeSensor):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)

def execute(self, context: Context):
def execute(self, context: Context) -> NoReturn:
trigger = DateTimeTrigger(moment=timezone.parse(self.target_time))
self.defer(
trigger=trigger,
method_name="execute_complete",
)

def execute_complete(self, context, event=None):
def execute_complete(self, context, event=None) -> None:
"""Execute when the trigger fires - returns immediately."""
return None
6 changes: 3 additions & 3 deletions airflow/sensors/time_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# under the License.
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, NoReturn

from airflow.exceptions import AirflowSkipException
from airflow.sensors.base import BaseSensorOperator
Expand Down Expand Up @@ -66,7 +66,7 @@ class TimeDeltaSensorAsync(TimeDeltaSensor):

"""

def execute(self, context: Context):
def execute(self, context: Context) -> NoReturn:
target_dttm = context["data_interval_end"]
target_dttm += self.delta
try:
Expand All @@ -78,6 +78,6 @@ def execute(self, context: Context):

self.defer(trigger=trigger, method_name="execute_complete")

def execute_complete(self, context, event=None):
def execute_complete(self, context, event=None) -> None:
"""Execute for when the trigger fires - return immediately."""
return None
4 changes: 2 additions & 2 deletions airflow/sensors/time_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from __future__ import annotations

import datetime
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, NoReturn

from airflow.sensors.base import BaseSensorOperator
from airflow.triggers.temporal import DateTimeTrigger
Expand Down Expand Up @@ -72,7 +72,7 @@ def __init__(self, *, target_time: datetime.time, **kwargs) -> None:

self.target_datetime = timezone.convert_to_utc(aware_time)

def execute(self, context: Context):
def execute(self, context: Context) -> NoReturn:
trigger = DateTimeTrigger(moment=self.target_datetime)
self.defer(
trigger=trigger,
Expand Down