You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/12/20 15:18:26 UTC

[GitHub] [airflow] raphaelauv opened a new pull request #20411: Add test on skip_all_except

raphaelauv opened a new pull request #20411:
URL: https://github.com/apache/airflow/pull/20411


   Add a test on skip_all_except and also allow None and not only empty iterable object.
   
   ```python
   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 >> [DummyOperator(task_id='dummy'), task_2, task_3 ]
   ```
   
   fail with :
   
   ```log
   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 allow
   ```python
   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 ]
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] raphaelauv commented on a change in pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
raphaelauv commented on a change in pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#discussion_r775029826



##########
File path: tests/models/test_skipmixin.py
##########
@@ -117,3 +118,28 @@ def get_state(ti):
 
         assert get_state(ti2) == State.NONE
         assert get_state(ti3) == State.SKIPPED
+
+    @pytest.mark.parametrize("branch_task_ids", [None, []])
+    def test_skip_all_except_none(self, dag_maker, branch_task_ids):

Review comment:
       thanks for the code example




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] raphaelauv commented on pull request #20411: Allow none skip in BranchOperator

Posted by GitBox <gi...@apache.org>.
raphaelauv commented on pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#issuecomment-997471522


   returning an empty list will do the work
   
   ```python
   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 ]
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] raphaelauv removed a comment on pull request #20411: Add test on skip_all_except

Posted by GitBox <gi...@apache.org>.
raphaelauv removed a comment on pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#issuecomment-997471522


   returning an empty list will do the work
   
   ```python
   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 ]
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] raphaelauv commented on pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
raphaelauv commented on pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#issuecomment-1008596484


   I just rebased


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] eladkal commented on a change in pull request #20411: Add test on skip_all_except

Posted by GitBox <gi...@apache.org>.
eladkal commented on a change in pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#discussion_r772478077



##########
File path: airflow/models/skipmixin.py
##########
@@ -140,6 +140,8 @@ def skip_all_except(self, ti: TaskInstance, branch_task_ids: Union[str, Iterable
         self.log.info("Following branch %s", branch_task_ids)
         if isinstance(branch_task_ids, str):
             branch_task_ids = {branch_task_ids}
+        elif branch_task_ids is None:
+            branch_task_ids = {}

Review comment:
       see https://github.com/apache/airflow/issues/18454 and https://github.com/apache/airflow/pull/18471
   If we now we are handling None then we need also to change
   https://github.com/apache/airflow/blob/9b05f027c3411ff5a5fec71ccf6826207ff9016d/airflow/operators/python.py#L221
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] eladkal commented on pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
eladkal commented on pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#issuecomment-1068920582


   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)
   https://github.com/apache/airflow/blob/db76f0b0dcc3852a6b20504017415a7e732da932/docs/apache-airflow/concepts/dags.rst#L317


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] uranusjr commented on a change in pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
uranusjr commented on a change in pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#discussion_r773562188



##########
File path: airflow/operators/python.py
##########
@@ -217,8 +217,10 @@ def execute(self, context: Context) -> Any:
             branches = {branch}
         elif isinstance(branch, list):
             branches = set(branch)
+        elif branch is None:
+            branches = set()
         else:
-            raise AirflowException("Branch callable must return either a task ID or a list of IDs")
+            raise AirflowException("Branch callable must return either None or a task ID or a list of IDs")

Review comment:
       ```suggestion
               raise AirflowException("Branch callable must return either None, a task ID, or a list of IDs")
   ```

##########
File path: airflow/models/skipmixin.py
##########
@@ -140,6 +140,8 @@ def skip_all_except(self, ti: TaskInstance, branch_task_ids: Union[str, Iterable
         self.log.info("Following branch %s", branch_task_ids)
         if isinstance(branch_task_ids, str):
             branch_task_ids = {branch_task_ids}
+        elif branch_task_ids is None:
+            branch_task_ids = {}

Review comment:
       ```suggestion
               branch_task_ids = ()
   ```
   
   Creating a dict is needless.

##########
File path: airflow/models/skipmixin.py
##########
@@ -128,7 +128,7 @@ def skip(
                 session=session,
             )
 
-    def skip_all_except(self, ti: TaskInstance, branch_task_ids: Union[str, Iterable[str]]):
+    def skip_all_except(self, ti: TaskInstance, branch_task_ids: Optional[Union[str, Iterable[str]]]):

Review comment:
       ```suggestion
       def skip_all_except(self, ti: TaskInstance, branch_task_ids: Union[None, str, Iterable[str]]):
   ```

##########
File path: tests/models/test_skipmixin.py
##########
@@ -117,3 +117,51 @@ def get_state(ti):
 
         assert get_state(ti2) == State.NONE
         assert get_state(ti3) == State.SKIPPED
+
+    def test_skip_all_except_none(self, dag_maker):

Review comment:
       You can avoid all these new tests (which duplicate most code in `test_skip_all_except`) with `pytest.mark.parametrize`.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] raphaelauv commented on a change in pull request #20411: Add test on skip_all_except

Posted by GitBox <gi...@apache.org>.
raphaelauv commented on a change in pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#discussion_r772512396



##########
File path: airflow/models/skipmixin.py
##########
@@ -140,6 +140,8 @@ def skip_all_except(self, ti: TaskInstance, branch_task_ids: Union[str, Iterable
         self.log.info("Following branch %s", branch_task_ids)
         if isinstance(branch_task_ids, str):
             branch_task_ids = {branch_task_ids}
+        elif branch_task_ids is None:
+            branch_task_ids = {}

Review comment:
       thanks for the review @eladka




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] raphaelauv commented on pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
raphaelauv commented on pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#issuecomment-998989935


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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#issuecomment-1008548082


   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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] raphaelauv closed pull request #20411: Allow none skip in BranchOperator

Posted by GitBox <gi...@apache.org>.
raphaelauv closed pull request #20411:
URL: https://github.com/apache/airflow/pull/20411


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] uranusjr commented on a change in pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
uranusjr commented on a change in pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#discussion_r773693784



##########
File path: tests/models/test_skipmixin.py
##########
@@ -117,3 +118,28 @@ def get_state(ti):
 
         assert get_state(ti2) == State.NONE
         assert get_state(ti3) == State.SKIPPED
+
+    @pytest.mark.parametrize("branch_task_ids", [None, []])
+    def test_skip_all_except_none(self, dag_maker, branch_task_ids):

Review comment:
       You can merge this into `test_skip_all_except` if you also parametrise the expected states:
   
   ```python
   @pytest.mark.parametrize(
       "branch_task_ids, expected_states",
       [
           (["task2"], {"task2": State.NONE, "task3": State.SKIPPED}),
           ("task2", {"task2": State.NONE, "task3": State.SKIPPED}),
           (None, {"task2": State.SKIPPED, "task3": State.SKIPPED}),
           ([], {"task2": State.SKIPPED, "task3": State.SKIPPED}),
       ],
   )
   def test_skip_except(branch_task_ids, expected_states, [others fixtures...]):
       ...
   
       task_instances = {
           "task1": TI(task1, ...),
           "task2": TI(task2, ...),
           "task3": TI(task3, ...),
       }
   
       SkipMixin().skip_all_except(ti=ti1, branch_task_ids=branch_task_ids)
   
       ...
   
       executed_states = {
           task_id: get_state(task_instances[task_id])
           for task_id in expected_states
       }
       assert executed_states == expected_states
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] uranusjr merged pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
uranusjr merged pull request #20411:
URL: https://github.com/apache/airflow/pull/20411


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] raphaelauv commented on pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
raphaelauv commented on pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#issuecomment-1075051442


   @eladkal can we merge ? thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] eladkal commented on a change in pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
eladkal commented on a change in pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#discussion_r828890999



##########
File path: docs/apache-airflow/concepts/dags.rst
##########
@@ -314,7 +314,7 @@ The ``BranchPythonOperator`` can also be used with XComs allowing branching cont
 
 If you wish to implement your own operators with branching functionality, you can inherit from :class:`~airflow.operators.branch.BaseBranchOperator`, which behaves similarly to ``BranchPythonOperator`` but expects you to provide an implementation of the method ``choose_branch``.
 
-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::
+As with the callable for ``BranchPythonOperator``, this method can return the ID of a downstream task, or a list of task IDs, which will be run, and all others will be skipped. It can also return None to skip all downstream task::

Review comment:
       ```suggestion
   As with the callable for ``BranchPythonOperator``, this method can return the ID of a downstream task, or a list of task IDs, which will be run, and all others will be skipped. It can also return None to skip all downstream task:
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] raphaelauv commented on pull request #20411: Allow to except_skip None on BranchPythonOperator

Posted by GitBox <gi...@apache.org>.
raphaelauv commented on pull request #20411:
URL: https://github.com/apache/airflow/pull/20411#issuecomment-1002170816


   @uranusjr could you re-review pls , thank you


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org