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

Allow to except_skip None on BranchPythonOperator #20411

Merged

Conversation

raphaelauv
Copy link
Contributor

@raphaelauv raphaelauv commented Dec 19, 2021

Add a test on skip_all_except and also allow None and not only empty iterable object.

because currently this code

def skip_things() -> str:
        if NOTHING:
            return None
        elif SOMETHING_WEIRD:
            return "task_2"
        else:
            return "task_3"

branch_task = BranchPythonOperator(
    task_id='branch_task',
    python_callable=skip_things)

task_2 = ...
task_3 = ...

branch_task >> [task_2, task_3 ]

fail with :

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1165, in _run_raw_task
    self._prepare_and_execute_task_with_callbacks(context, task)
  File "/usr/local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1283, in _prepare_and_execute_task_with_callbacks
    result = self._execute_task(context, task_copy)
  File "/usr/local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1313, in _execute_task
    result = task_copy.execute(context=context)
  File "/usr/local/lib/python3.8/site-packages/airflow/operators/python.py", line 180, in execute
    self.skip_all_except(context['ti'], branch)
  File "/usr/local/lib/python3.8/site-packages/airflow/models/skipmixin.py", line 128, in skip_all_except
    branch_task_ids = set(branch_task_ids)
TypeError: 'NoneType' object is not iterable

Only an empty iterable is currently allow

def skip_things() -> str:
        if NOTHING:
            return []
        elif SOMETHING_WEIRD:
            return "task_2"
        else:
            return "task_3"

branch_task = BranchPythonOperator(
    task_id='branch_task',
    python_callable=skip_things)

task_2 = ...
task_3 = ...

branch_task >> [task_2, task_3 ]

@raphaelauv raphaelauv force-pushed the fix/allow_to_skip_zero_downstream_tasks branch 6 times, most recently from 6bdda79 to a864a38 Compare December 19, 2021 22:08
@raphaelauv raphaelauv closed this Dec 19, 2021
@raphaelauv raphaelauv deleted the fix/allow_to_skip_zero_downstream_tasks branch December 19, 2021 22:17
@raphaelauv raphaelauv changed the title Allow none skip in BranchOperator Add test on skip_all_except Dec 20, 2021
@raphaelauv raphaelauv restored the fix/allow_to_skip_zero_downstream_tasks branch December 20, 2021 15:18
@raphaelauv raphaelauv reopened this Dec 20, 2021
@raphaelauv raphaelauv force-pushed the fix/allow_to_skip_zero_downstream_tasks branch from df2091c to cfe8e63 Compare December 20, 2021 16:40
@raphaelauv raphaelauv changed the title Add test on skip_all_except Allow to except_skip None on BranchPythonOperator Dec 20, 2021
@raphaelauv raphaelauv force-pushed the fix/allow_to_skip_zero_downstream_tasks branch from cfe8e63 to 89671b9 Compare December 20, 2021 18:26
@raphaelauv
Copy link
Contributor Author

@eladkal could you re-review this PR , than you again

Copy link
Member

@uranusjr uranusjr left a comment

Choose a reason for hiding this comment

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

I’m personally skeptical to this since it’s much too easy for a user to accidentally return None from a function and cause tasks to unintentionally not run. But I guess this is a subjective issue and not a designe is objectively superior.

airflow/models/skipmixin.py Outdated Show resolved Hide resolved
airflow/models/skipmixin.py Outdated Show resolved Hide resolved
airflow/operators/python.py Outdated Show resolved Hide resolved
tests/models/test_skipmixin.py Outdated Show resolved Hide resolved
@raphaelauv raphaelauv force-pushed the fix/allow_to_skip_zero_downstream_tasks branch from 828ba69 to 662ee9e Compare December 22, 2021 08:17
@raphaelauv raphaelauv force-pushed the fix/allow_to_skip_zero_downstream_tasks branch 2 times, most recently from 93f620d to c3b95a3 Compare December 24, 2021 15:08
@raphaelauv
Copy link
Contributor Author

@uranusjr could you re-review pls , thank you

@github-actions
Copy link

The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest main at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.

@github-actions github-actions bot added the full tests needed We need to run full set of tests for this PR to merge label Jan 10, 2022
@raphaelauv raphaelauv force-pushed the fix/allow_to_skip_zero_downstream_tasks branch from c3b95a3 to 8277776 Compare January 10, 2022 07:17
@raphaelauv
Copy link
Contributor Author

I just rebased

@raphaelauv raphaelauv force-pushed the fix/allow_to_skip_zero_downstream_tasks branch from 8277776 to 073865e Compare February 6, 2022 15:20
@eladkal
Copy link
Contributor

eladkal commented Mar 16, 2022

Can you please explain the behavior of returning None in the docs? (I'm not sure if we have a designated doc specifically for this operator)

As with the callable for ``BranchPythonOperator``, this method should return the ID of a downstream task, or a list of task IDs, which will be run, and all others will be skipped::

@eladkal eladkal added this to the Airflow 2.3.0 milestone Mar 16, 2022
@raphaelauv raphaelauv force-pushed the fix/allow_to_skip_zero_downstream_tasks branch from 4012f06 to 2fdbf3e Compare March 17, 2022 08:39
@raphaelauv raphaelauv requested a review from potiuk as a code owner March 17, 2022 08:39
Co-authored-by: eladkal <45845474+eladkal@users.noreply.github.com>
Copy link
Contributor

@eladkal eladkal left a comment

Choose a reason for hiding this comment

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

LGTM

@raphaelauv
Copy link
Contributor Author

@eladkal can we merge ? thanks

@uranusjr uranusjr merged commit fa655fe into apache:main Mar 22, 2022
@raphaelauv raphaelauv deleted the fix/allow_to_skip_zero_downstream_tasks branch March 22, 2022 11:37
@ephraimbuddy ephraimbuddy added the type:bug-fix Changelog: Bug Fixes label Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full tests needed We need to run full set of tests for this PR to merge type:bug-fix Changelog: Bug Fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants