This won't work.
In Airflow each operator has execute function that set the operator logic. In case of BaseBranchOperator
the execute
function leverages choose_branch
and handle the logic of how to skip tasks, so all user left to do is just say what task to skip and this is done in choose_branch
:
def choose_branch(self, context: Context) -> str | Iterable[str]:
"""
Abstract method to choose which branch to run.
Subclasses should implement this, running whatever logic is
necessary to choose a branch and returning a task_id or list of
task_ids.
:param context: Context dictionary as passed to execute()
"""
raise NotImplementedError
So when you want to implement your own branch operator all you need to do is inherit from BaseBranchOperator
and override the choose_branch
function.
You can decide that you don't want this mechanism and you want to build your own branch logic which means that you will need to implement the logic of how to skip tasks. In that case you will implement MyBaseBranchOperator
and then your actual branch operator (In your case MyOperator
) will be:
class MyOperator(MyBaseBranchOperator):
...
I think what you are really after is pre_execute()
which triggers right before execute()
is called
So probably what you want is:
class MyOperator(BaseBranchOperator):
def pre_execute(self, context):
print('hi')
def choose_branch(self, context):
if True:
return 'task_A'
else:
return 'task_B'