-
Notifications
You must be signed in to change notification settings - Fork 17k
Set dependencies in MappedOperator via XComArgs #20931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,12 +17,14 @@ | |||||
| from typing import TYPE_CHECKING, Any, List, Optional, Sequence, Union | ||||||
|
|
||||||
| from airflow.exceptions import AirflowException | ||||||
| from airflow.models.baseoperator import BaseOperator, MappedOperator | ||||||
| from airflow.models.taskmixin import DAGNode, DependencyMixin | ||||||
| from airflow.models.xcom import XCOM_RETURN_KEY | ||||||
| from airflow.utils.context import Context | ||||||
| from airflow.utils.edgemodifier import EdgeModifier | ||||||
|
|
||||||
| if TYPE_CHECKING: | ||||||
| from airflow.models.baseoperator import BaseOperator, MappedOperator | ||||||
|
|
||||||
|
|
||||||
| class XComArg(DependencyMixin): | ||||||
| """ | ||||||
|
|
@@ -59,9 +61,9 @@ class XComArg(DependencyMixin): | |||||
| :type key: str | ||||||
| """ | ||||||
|
|
||||||
| def __init__(self, operator: Union[BaseOperator, MappedOperator], key: str = XCOM_RETURN_KEY): | ||||||
| self._operator = operator | ||||||
| self._key = key | ||||||
| def __init__(self, operator: "Union[BaseOperator, MappedOperator]", key: str = XCOM_RETURN_KEY): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Any reason for this change?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid uncessary imports/reduce chance import cycles.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After #20945 we can probably change this to |
||||||
| self.operator = operator | ||||||
| self.key = key | ||||||
|
|
||||||
| def __eq__(self, other): | ||||||
| return self.operator == other.operator and self.key == other.key | ||||||
|
|
@@ -92,25 +94,15 @@ def __str__(self): | |||||
| xcom_pull = f"{{{{ task_instance.xcom_pull({xcom_pull_kwargs}) }}}}" | ||||||
| return xcom_pull | ||||||
|
|
||||||
| @property | ||||||
| def operator(self) -> Union[BaseOperator, MappedOperator]: | ||||||
| """Returns operator of this XComArg.""" | ||||||
| return self._operator | ||||||
|
|
||||||
| @property | ||||||
| def roots(self) -> List[DAGNode]: | ||||||
| """Required by TaskMixin""" | ||||||
| return [self._operator] | ||||||
| return [self.operator] | ||||||
|
|
||||||
| @property | ||||||
| def leaves(self) -> List[DAGNode]: | ||||||
| """Required by TaskMixin""" | ||||||
| return [self._operator] | ||||||
|
|
||||||
| @property | ||||||
| def key(self) -> str: | ||||||
| """Returns keys of this XComArg""" | ||||||
| return self._key | ||||||
| return [self.operator] | ||||||
|
|
||||||
| def set_upstream( | ||||||
| self, | ||||||
|
|
@@ -144,3 +136,23 @@ def resolve(self, context: Context) -> Any: | |||||
| resolved_value = resolved_value[0] | ||||||
|
|
||||||
| return resolved_value | ||||||
|
|
||||||
| @staticmethod | ||||||
| def apply_upstream_relationship(op: "Union[BaseOperator, MappedOperator]", arg: Any): | ||||||
| """ | ||||||
| Set dependency for XComArgs. | ||||||
|
|
||||||
| This looks for XComArg objects in ``arg`` "deeply" (looking inside lists, dicts and classes decorated | ||||||
| with "template_fields") and sets the relationship to ``op`` on any found. | ||||||
| """ | ||||||
| if isinstance(arg, XComArg): | ||||||
| op.set_upstream(arg.operator) | ||||||
| elif isinstance(arg, (tuple, set, list)): | ||||||
| for elem in arg: | ||||||
| XComArg.apply_upstream_relationship(op, elem) | ||||||
| elif isinstance(arg, dict): | ||||||
| for elem in arg.values(): | ||||||
| XComArg.apply_upstream_relationship(op, elem) | ||||||
| elif hasattr(arg, "template_fields"): | ||||||
| for elem in arg.template_fields: | ||||||
| XComArg.apply_upstream_relationship(op, elem) | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"badly" as part of the serialization code
airflow/airflow/serialization/serialized_objects.py
Lines 970 to 971 in 14a057f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be fixed/not needed by your #20945?