Skip to content
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

BaseBranchOperator will push to xcom by default. #13763

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion airflow/operators/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ def choose_branch(self, context: Dict) -> Union[str, Iterable[str]]:
raise NotImplementedError

def execute(self, context: Dict):
self.skip_all_except(context['ti'], self.choose_branch(context))
branches_to_execute = self.choose_branch(context)
self.skip_all_except(context['ti'], branches_to_execute)
return branches_to_execute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means it will always push right, even if do_xcom_push is False

Copy link
Contributor Author

@ashmeet13 ashmeet13 Jan 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this. I dont think it would always push.
I did run the test case that I added for checking xcom push (Permalink to the test function) with do_xcom_push=False and the value returned by - ti.xcom_pull(task_ids='make_choice') was None

Have I understood the issue wrong? I followed PythonBranchOperator as a guide to figure out what was needed to be done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes,

# If the task returns a result, push an XCom containing it
if task_copy.do_xcom_push and result is not None:
self.xcom_push(key=XCOM_RETURN_KEY, value=result)
return result

should take care of it

21 changes: 21 additions & 0 deletions tests/operators/test_branch_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,24 @@ def test_with_skip_in_branch_downstream_dependencies(self):
assert ti.state == State.NONE
else:
raise Exception

def test_xcom_push(self):
self.branch_op = ChooseBranchOne(task_id='make_choice', dag=self.dag)

self.branch_1.set_upstream(self.branch_op)
self.branch_2.set_upstream(self.branch_op)
self.dag.clear()

dr = self.dag.create_dagrun(
run_type=DagRunType.MANUAL,
start_date=timezone.utcnow(),
execution_date=DEFAULT_DATE,
state=State.RUNNING,
)

self.branch_op.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)

tis = dr.get_task_instances()
for ti in tis:
if ti.task_id == 'make_choice':
assert ti.xcom_pull(task_ids='make_choice') == 'branch_1'