You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by as...@apache.org on 2021/06/11 13:10:36 UTC

[airflow] branch main updated: Correctly set `dag.fileloc` when using the `@dag` decorator (#16384)

This is an automated email from the ASF dual-hosted git repository.

ash pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 4ef804f  Correctly set `dag.fileloc` when using the `@dag` decorator (#16384)
4ef804f is described below

commit 4ef804ffa2c3042ca49a3beeaa745e068325d51b
Author: Ash Berlin-Taylor <as...@firemirror.com>
AuthorDate: Fri Jun 11 14:10:11 2021 +0100

    Correctly set `dag.fileloc` when using the `@dag` decorator (#16384)
    
    Previously this was always showing up as `airflow/models/dag.py`!
---
 airflow/models/dag.py    |  4 ++++
 tests/models/test_dag.py | 10 ++++++++++
 2 files changed, 14 insertions(+)

diff --git a/airflow/models/dag.py b/airflow/models/dag.py
index 15e843c..e9cc042 100644
--- a/airflow/models/dag.py
+++ b/airflow/models/dag.py
@@ -2348,6 +2348,10 @@ def dag(*dag_args, **dag_kwargs):
                 for name, value in f_sig.arguments.items():
                     f_kwargs[name] = dag_obj.param(name, value)
 
+                # set file location to caller source path
+                back = sys._getframe().f_back
+                dag_obj.fileloc = back.f_code.co_filename if back else ""
+
                 # Invoke function to create operators in the DAG scope.
                 f(**f_kwargs)
 
diff --git a/tests/models/test_dag.py b/tests/models/test_dag.py
index 90892fc..462dad1 100644
--- a/tests/models/test_dag.py
+++ b/tests/models/test_dag.py
@@ -1805,6 +1805,16 @@ class TestDagDecorator(unittest.TestCase):
         super().tearDown()
         clear_db_runs()
 
+    def test_fileloc(self):
+        @dag_decorator(default_args=self.DEFAULT_ARGS)
+        def noop_pipeline():
+            ...
+
+        dag = noop_pipeline()
+        assert isinstance(dag, DAG)
+        assert dag.dag_id, 'noop_pipeline'
+        assert dag.fileloc == __file__
+
     def test_set_dag_id(self):
         """Test that checks you can set dag_id from decorator."""