turbaszek commented on a change in pull request #8652: URL: https://github.com/apache/airflow/pull/8652#discussion_r420203617
########## File path: airflow/models/xcom_arg.py ########## @@ -0,0 +1,149 @@ +# 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 typing import Any, Dict, List, Union + +from airflow.exceptions import AirflowException +from airflow.models.baseoperator import BaseOperator # pylint: disable=R0401 +from airflow.models.xcom import XCOM_RETURN_KEY + + +class XComArg: + """ + Class that represents a XCom push from a previous operator. + Defaults to "return_value" as only key. + + Current implementations supports + xcomarg >> op + xcomarg << op + op >> xcomarg (by BaseOperator code) + op << xcomarg (by BaseOperator code) + + **Example**: The moment you get a result from any operator (functional or regular) you can :: + + any_op = AnyOperator() + xcomarg = XComArg(any_op) + # or equivalently + xcomarg = any_op.output + my_op = MyOperator() + my_op >> xcomarg + + This object can be used in legacy Operators via Jinja. + + **Example**: You can make this result to be part of any generated string :: + + any_op = AnyOperator() + xcomarg = any_op.output + op1 = MyOperator(my_text_message=f"the value is {xcomarg}") + op2 = MyOperator(my_text_message=f"the value is {xcomarg['topic']}") + + :param operator: operator to which the XComArg belongs to + :type operator: airflow.models.baseoperator.BaseOperator + :param key: key value which is used for xcom_pull (key in the XCom table) + :type key: str + """ + + def __init__(self, operator: BaseOperator, key: str = XCOM_RETURN_KEY): + self._operator = operator + self._key = key + + def __eq__(self, other): + return (self.operator == other.operator + and self.key == other.key) + + def __lshift__(self, other): + """ + Implements xcomresult << op Review comment: ```suggestion Implements XComArg << op ``` ########## File path: airflow/models/xcom_arg.py ########## @@ -0,0 +1,149 @@ +# 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 typing import Any, Dict, List, Union + +from airflow.exceptions import AirflowException +from airflow.models.baseoperator import BaseOperator # pylint: disable=R0401 +from airflow.models.xcom import XCOM_RETURN_KEY + + +class XComArg: + """ + Class that represents a XCom push from a previous operator. + Defaults to "return_value" as only key. + + Current implementations supports + xcomarg >> op + xcomarg << op + op >> xcomarg (by BaseOperator code) + op << xcomarg (by BaseOperator code) + + **Example**: The moment you get a result from any operator (functional or regular) you can :: + + any_op = AnyOperator() + xcomarg = XComArg(any_op) + # or equivalently + xcomarg = any_op.output + my_op = MyOperator() + my_op >> xcomarg + + This object can be used in legacy Operators via Jinja. + + **Example**: You can make this result to be part of any generated string :: + + any_op = AnyOperator() + xcomarg = any_op.output + op1 = MyOperator(my_text_message=f"the value is {xcomarg}") + op2 = MyOperator(my_text_message=f"the value is {xcomarg['topic']}") + + :param operator: operator to which the XComArg belongs to + :type operator: airflow.models.baseoperator.BaseOperator + :param key: key value which is used for xcom_pull (key in the XCom table) + :type key: str + """ + + def __init__(self, operator: BaseOperator, key: str = XCOM_RETURN_KEY): + self._operator = operator + self._key = key + + def __eq__(self, other): + return (self.operator == other.operator + and self.key == other.key) + + def __lshift__(self, other): + """ + Implements xcomresult << op + """ + self.set_upstream(other) + return self + + def __rshift__(self, other): + """ + Implements xcomresult >> op Review comment: ```suggestion Implements XComArg >> op ``` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
