Skip to content

Is @DAG/@task decorators compatible with dynamically generation? #21278

Answered by ashb
makrushin-evgenii asked this question in General
Discussion options

You must be logged in to vote

This isn't anything to do with the TaskFlow directly, it's how python scoping works with for-loops, coupled with now DAGs are parsed.

  • When parsing the DAG file (which happens up front) test_task(dag_id) will be called with 1..3 as the values
  • At this point, the value of the dag_id variable is left at test_dag_3
  • Then Airflow says "I want to run dag X", pulls the dag out of the module.

So when the task function is executed, the current/latest value of dag_id variable is the one you get.

The way to do this and get the behaviour you want is to use dag from the context:

    @dag(dag_id=dag_id, schedule_interval=None, start_date=datetime(2022, 2, 1), catchup=False)
    def test_dag():
        …

Replies: 3 comments 7 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@makrushin-evgenii
Comment options

@potiuk
Comment options

@potiuk
Comment options

@potiuk
Comment options

Comment options

You must be logged in to vote
3 replies
@potiuk
Comment options

@uranusjr
Comment options

@makrushin-evgenii
Comment options

Answer selected by makrushin-evgenii
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug This is a clearly a bug area:core
4 participants
Converted from issue

This discussion was converted from issue #21274 on February 02, 2022 20:38.