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 2018/09/28 16:51:35 UTC

[GitHub] aoen closed pull request #3956: [AIRFLOW-3123] Allow nested use of DAG as a context manager

aoen closed pull request #3956: [AIRFLOW-3123] Allow nested use of  DAG as a context manager
URL: https://github.com/apache/incubator-airflow/pull/3956
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/airflow/models.py b/airflow/models.py
index a2de010c51..e4dffd3b47 100755
--- a/airflow/models.py
+++ b/airflow/models.py
@@ -3391,7 +3391,7 @@ def __init__(
         self.on_success_callback = on_success_callback
         self.on_failure_callback = on_failure_callback
 
-        self._context_manager_set = False
+        self._old_context_manager_dags = []
 
         self._comps = {
             'dag_id',
@@ -3440,16 +3440,13 @@ def __hash__(self):
 
     def __enter__(self):
         global _CONTEXT_MANAGER_DAG
-        if not self._context_manager_set:
-            self._old_context_manager_dag = _CONTEXT_MANAGER_DAG
-            _CONTEXT_MANAGER_DAG = self
-            self._context_manager_set = True
+        self._old_context_manager_dags.append(_CONTEXT_MANAGER_DAG)
+        _CONTEXT_MANAGER_DAG = self
         return self
 
     def __exit__(self, _type, _value, _tb):
         global _CONTEXT_MANAGER_DAG
-        _CONTEXT_MANAGER_DAG = self._old_context_manager_dag
-        self._context_manager_set = False
+        _CONTEXT_MANAGER_DAG = self._old_context_manager_dags.pop()
 
     # /Context Manager ----------------------------------------------
 
diff --git a/tests/models.py b/tests/models.py
index 999b1be1bb..7e3c2929e0 100644
--- a/tests/models.py
+++ b/tests/models.py
@@ -141,11 +141,13 @@ def test_dag_as_context_manager(self):
         with dag:
             with dag:
                 op7 = DummyOperator(task_id='op7')
-        op8 = DummyOperator(task_id='op8')
-        op8.dag = dag2
+            op8 = DummyOperator(task_id='op8')
+        op9 = DummyOperator(task_id='op8')
+        op9.dag = dag2
 
         self.assertEqual(op7.dag, dag)
-        self.assertEqual(op8.dag, dag2)
+        self.assertEqual(op8.dag, dag)
+        self.assertEqual(op9.dag, dag2)
 
     def test_dag_topological_sort(self):
         dag = DAG(


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services