Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(
self.retry = retry
self.request_timeout = request_timeout
self.metadata = metadata
self.expected_statuses = self._normalize_state_list(expected_statuses)
self.expected_statuses = expected_statuses
self.project_id = project_id
self.gcp_cloud_conn_id = gcp_conn_id
self.impersonation_chain = impersonation_chain
Expand Down Expand Up @@ -144,4 +144,4 @@ def poke(self, context: Context) -> bool:
if run.state in (TransferState.FAILED, TransferState.CANCELLED):
message = f"Transfer {self.run_id} did not succeed"
raise AirflowException(message)
return run.state in self.expected_statuses
return run.state in self._normalize_state_list(self.expected_statuses)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two small consequences of moving the normalisation here, neither blocking:

Error quality. _normalize_state_list does TransferState[state.upper()], so a typo'd status now surfaces as a bare KeyError: 'SUCEEDED' from inside the sensor on every poke, rather than at parse time where the traceback at least pointed at the Dag file. That was survivable when it failed fast at parse; as a runtime failure it's worth wrapping in a ValueError naming the offending value and the valid ones. Arguably out of scope here, but this PR is what makes it user-visible.

Repeated work. Normalisation now runs on every poke rather than once. It's cheap and a sensor is I/O-bound, so this is genuinely negligible — mentioning it only so it's a conscious choice rather than an oversight. If you'd rather avoid it, normalising once into a cached attribute on first poke would do it.


Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting

Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,20 @@ def test_poke_returns_true(self, mock_hook):
retry=DEFAULT,
timeout=None,
)

@mock.patch(
"airflow.providers.google.cloud.sensors.bigquery_dts.BiqQueryDataTransferServiceHook",
return_value=MM(get_transfer_run=MM(return_value=MM(state=TransferState.SUCCEEDED))),
)
def test_templated_expected_statuses_normalized_at_poke_time(self, mock_hook):
op = BigQueryDataTransferServiceTransferRunSensor(
transfer_config_id=TRANSFER_CONFIG_ID,
run_id=RUN_ID,
task_id="id",
project_id=PROJECT_ID,
expected_statuses="{{ var.value.expected_status }}",
)
# Template rendering replaces the Jinja expression with the resolved value before poke.
op.expected_statuses = "succeeded"

assert op.poke({}) is True
1 change: 0 additions & 1 deletion scripts/ci/prek/validate_operators_init_exemptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ providers/google/src/airflow/providers/google/cloud/operators/dataproc.py::Datap
providers/google/src/airflow/providers/google/cloud/operators/dataproc.py::DataprocSubmitJobOperator
providers/google/src/airflow/providers/google/cloud/operators/functions.py::CloudFunctionDeployFunctionOperator
providers/google/src/airflow/providers/google/cloud/operators/gcs.py::GCSFileTransformOperator
providers/google/src/airflow/providers/google/cloud/sensors/bigquery_dts.py::BigQueryDataTransferServiceTransferRunSensor
providers/google/src/airflow/providers/google/cloud/sensors/cloud_composer.py::CloudComposerExternalTaskSensor
providers/google/src/airflow/providers/google/cloud/transfers/azure_fileshare_to_gcs.py::AzureFileShareToGCSOperator
providers/google/src/airflow/providers/google/cloud/transfers/gcs_to_bigquery.py::GCSToBigQueryOperator
Expand Down