A Python context manager uses the with keyword to manage resources through __enter__() and __exit__(). In Airflow, it defines a DAG scope so tasks can reference the active DAG without receiving a dag argument. section 2.2
Defining an Airflow DAG
with DAG(dag_id="download_rocket_launches", start_date=..., schedule=None): download_launches = BashOperator(task_id="download_launches", ...) get_pictures = PythonOperator(task_id="get_pictures", ...) download_launches >> get_pictures >> notify section 2.2
The context-manager approach trades explicitness for less repetitive DAG wiring:
- Reduces verbosity because tasks do not repeatedly receive the DAG object. section 2.2
- Limits the context-manager scope to one DAG, which can restrict files defining multiple workflows. section 2.2
- Provides an alternative to explicit DAG definitions and the Taskflow API. section 2.2
Listing 2.3 Instantiating a DAG object
with DAG( #1
dag_id=02_download_rocket_launches", #2
start_date=pendulum.today('UTC').add(days=-14), #3
schedule=None,
):
Inspect the compact DAG instantiation that establishes the context for its tasks. section 2.2



