airflow.models.xcom_arg
¶
Module Contents¶
Classes¶
Class that represents a XCom push from a previous operator. |
- class airflow.models.xcom_arg.XComArg(operator, key=XCOM_RETURN_KEY)[source]¶
Bases:
airflow.models.taskmixin.DependencyMixin
Class that represents a XCom push from a previous operator. Defaults to “return_value” as only key.
- Current implementation 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 (decorated 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']}")
- Parameters
operator (airflow.models.operator.Operator) – operator to which the XComArg belongs to
key (str) – key value which is used for xcom_pull (key in the XCom table)
- __iter__(self)[source]¶
Override iterable protocol to raise error explicitly.
The default
__iter__
implementation in Python calls__getitem__
with 0, 1, 2, etc. until it hits anIndexError
. This does not work well with our custom__getitem__
implementation, and results in poor DAG-writing experience since a misplaced*
expansion would create an infinite loop consuming the entire DAG parser.This override catches the error eagerly, so an incorrectly implemented DAG fails fast and avoids wasting resources on nonsensical iterating.
- __str__(self)[source]¶
Backward compatibility for old-style jinja used in Airflow Operators
Example: to use XComArg at BashOperator:
BashOperator(cmd=f"... { xcomarg } ...")
- Returns
- set_upstream(self, task_or_task_list, edge_modifier=None)[source]¶
Proxy to underlying operator set_upstream method. Required by TaskMixin.
- set_downstream(self, task_or_task_list, edge_modifier=None)[source]¶
Proxy to underlying operator set_downstream method. Required by TaskMixin.
- resolve(self, context, session=NEW_SESSION)[source]¶
Pull XCom value for the existing arg. This method is run during
op.execute()
in respectable context.