uranusjr commented on code in PR #41325: URL: https://github.com/apache/airflow/pull/41325#discussion_r1823779271
########## airflow/decorators/assets.py: ########## @@ -0,0 +1,129 @@ +# 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 inspect +import types +from typing import TYPE_CHECKING, Any, Iterator, Mapping + +import attrs + +from airflow.assets import Asset, _validate_identifier +from airflow.models.dag import DAG, ScheduleArg +from airflow.operators.python import PythonOperator + +if TYPE_CHECKING: + from airflow.io.path import ObjectStoragePath + + [email protected](kw_only=True) +class AssetRef: + """Reference to an asset.""" + + name: str + + +class _AssetMainOperator(PythonOperator): + def __init__(self, *, definition_name: str, **kwargs) -> None: + super().__init__(**kwargs) + self._definition_name = definition_name + + def _iter_kwargs(self, context: Mapping[str, Any]) -> Iterator[tuple[str, Any]]: + for key in inspect.signature(self.python_callable).parameters: + if key == "self": + value: Any = AssetRef(name=self._definition_name) + elif key == "context": + value = context + else: + # TODO: This does not check if the upstream asset actually + # exists. Should we do a second pass in the DAG processor to + # raise parse-time errors if a non-existent asset is referenced? + # How? Should we also fail the task at runtime? Or should the + # dangling reference simply do nothing? + value = AssetRef(name=key) + yield key, value + + def determine_kwargs(self, context: Mapping[str, Any]) -> Mapping[str, Any]: + return dict(self._iter_kwargs(context)) + + [email protected](kw_only=True) +class AssetDefinition(Asset): + """ + Asset representation from decorating a function with ``@asset``. + + :meta private: + """ + + function: types.FunctionType + schedule: ScheduleArg + + def __attrs_post_init__(self) -> None: + parameters = inspect.signature(self.function).parameters + with DAG(dag_id=self.name, schedule=self.schedule, auto_register=True) as dag: + _AssetMainOperator( + task_id="__main__", + inlets=[ + Asset(name=inlet_aset_name) + for inlet_aset_name in parameters + if inlet_aset_name not in ("self", "context") + ], Review Comment: I don’t think this is good enough since `Asset(name="a")` and `Asset(name="a", uri="somewhere")` are considered different assets. Here we only have the name, so maybe one way to do this would be to add a stub (AssetRef?) here, and do a second pass in the DAG processor to get the actual assets? Or maybe we should just keep the AssetRef here, and add code to task execution at runtime to resolve it into an actual asset, similar to how we handle AssetAlias. -- 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]
