hussein-awala commented on code in PR #70225: URL: https://github.com/apache/airflow/pull/70225#discussion_r4406906808
########## providers/standard/src/airflow/providers/standard/sensors/asset.py: ########## @@ -0,0 +1,282 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import contextlib +from datetime import timedelta +from typing import TYPE_CHECKING, Any, ClassVar, Literal, Protocol, cast + +from pydantic import BaseModel + +from airflow.providers.common.compat.sdk import ( + AirflowOptionalProviderFeatureException, + BaseSensorOperator, + PokeReturnValue, +) +from airflow.providers.standard.version_compat import AIRFLOW_V_3_2_PLUS, AIRFLOW_V_3_4_PLUS + +if not AIRFLOW_V_3_2_PLUS: + raise AirflowOptionalProviderFeatureException("AssetEventSensor needs Airflow 3.2+.") + +if TYPE_CHECKING: + from collections.abc import Callable, Sequence + from datetime import datetime + + from airflow.providers.common.compat.sdk import Context + from airflow.sdk.definitions.asset import Asset, AssetAlias, AssetRef + from airflow.sdk.execution_time.comms import AssetEventResult + from airflow.sdk.execution_time.context import InletEventsAccessor + + +def _serialize_events(events: list[Any]) -> list[Any]: + """ + Convert asset events Pydantic models to JSON-compatible dicts. + + This keeps the XCom value plain JSON, rather than relying on how XCom + serializes pydantic models. + """ + return [e.model_dump(mode="json") if isinstance(e, BaseModel) else e for e in events] + + +class AssetEventTimeBound(Protocol): + """ + A time bound that Airflow resolves during template rendering. + + Implement ``resolve(context)`` to return a datetime or ISO string. Custom + objects do not need to inherit this protocol. + The result must stay fixed across reschedules of the same Dag run. + """ + + def resolve(self, context: Context) -> datetime | str: + """Return the event timestamp bound for this task's runtime context.""" + ... + + +class _ContextTimeBound(AssetEventTimeBound): + _context_key: ClassVar[str] Review Comment: I made `_get_timestamp()` the single abstract hook and added a separate error label. This removes the lookup cast. The built-in helpers now provide `serialize()` with anchor and offset_seconds fields. Mapped arguments still use the core serializer’s representation fallback, so the helpers retain a deterministic `__repr__`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
