Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a note and an example clarifying the usage of DAG-level params #40541

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions docs/apache-airflow/core-concepts/params.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ To add Params to a :class:`~airflow.models.dag.DAG`, initialize it with the ``pa
Use a dictionary that maps Param names to either a :class:`~airflow.models.param.Param` or an object indicating the parameter's default value.

.. code-block::
:emphasize-lines: 6-9
:emphasize-lines: 7-10
from airflow import DAG
from airflow.decorators import task
from airflow.models.param import Param
with DAG(
Expand All @@ -47,7 +48,34 @@ Use a dictionary that maps Param names to either a :class:`~airflow.models.param
"x": Param(5, type="integer", minimum=3),
"my_int_param": 6
},
):
) as dag:
@task.python
def example_task(params: dict):
# This will print the default value, 6:
dag.log.info(dag.params['my_int_param'])
# This will print the manually-provided value, 42:
dag.log.info(params['my_int_param'])
# This will print the default value, 5, since it wasn't provided manually:
dag.log.info(params['x'])
example_task()
if __name__ == "__main__":
dag.test(
run_conf={"my_int_param": 42}
)
.. note::

DAG-level parameters are the default values passed on to tasks. These should not be confused with values manually
provided through the UI form or CLI, which exist solely within the context of a :class:`~airflow.models.dagrun.DagRun`
and a :class:`~airflow.models.taskinstance.TaskInstance`. This distinction is crucial for TaskFlow DAGs, which may
include logic within the ``with DAG(...) as dag:`` block. In such cases, users might try to access the manually-provided
parameter values using the ``dag`` object, but this will only ever contain the default values. To ensure that the
manually-provided values are accessed, use a template variable such as ``params`` or ``ti`` within your task.

Task-level Params
-----------------
Expand Down