Skip to content

Update dag-run.rst to ref dag_run.conf#24452

Closed
patricker wants to merge 3 commits intoapache:mainfrom
patricker:patch-1
Closed

Update dag-run.rst to ref dag_run.conf#24452
patricker wants to merge 3 commits intoapache:mainfrom
patricker:patch-1

Conversation

@patricker
Copy link
Contributor

Docs made it sound like there was no way to access dag_run.conf outside of a templated field.

Docs made it sound like there was no way to access dag_run.conf outside of a templated field.
Comment on lines 272 to 292
import pendulum

from airflow import DAG
from airflow.operators.python import PythonOperator

dag = DAG(
"example_parameterized_dag",
schedule_interval=None,
start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
catchup=False,
)

def print_conf(**context):
conf1 = context['dag_run'].conf['conf1']
print(conf1)

parameterized_task = PythonOperator(
task_id="parameterized_task",
python_callable=print_conf,
dag=dag,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import pendulum
from airflow import DAG
from airflow.operators.python import PythonOperator
dag = DAG(
"example_parameterized_dag",
schedule_interval=None,
start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
catchup=False,
)
def print_conf(**context):
conf1 = context['dag_run'].conf['conf1']
print(conf1)
parameterized_task = PythonOperator(
task_id="parameterized_task",
python_callable=print_conf,
dag=dag,
)
import pendulum
from airflow import DAG
dag = DAG("example_parameterized_dag",
schedule_interval=None,
start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
catchup=False,
)
@dag.task(task_id="parameterized_task")
def print_conf(dag_run=None):
conf1 = dag_run.conf["conf1"]
print(conf1)
print_conf()

We've moved away from using PythonOperator with preference for the TaskFlow API. I don't think the docs overall reflect this (...yet) but the example DAGs were all updated with this in mind. Also with the TaskFlow API you can access context variables directly without needing kwargs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote it up slightly differently. Are you OK with my use of context = get_current_context()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_current_context() was really purpose-built to access context variables deep in the stack without having to pass them around from the task callable to other callables.

But, maybe another example could be added to access dag_run.conf in a callable that isn't a task using get_current_context()? I don't think more examples could hurt 🙂

Comment on lines 272 to 292
import pendulum

from airflow import DAG
from airflow.operators.python import PythonOperator

dag = DAG(
"example_parameterized_dag",
schedule_interval=None,
start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
catchup=False,
)

def print_conf(**context):
conf1 = context['dag_run'].conf['conf1']
print(conf1)

parameterized_task = PythonOperator(
task_id="parameterized_task",
python_callable=print_conf,
dag=dag,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import pendulum
from airflow import DAG
from airflow.operators.python import PythonOperator
dag = DAG(
"example_parameterized_dag",
schedule_interval=None,
start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
catchup=False,
)
def print_conf(**context):
conf1 = context['dag_run'].conf['conf1']
print(conf1)
parameterized_task = PythonOperator(
task_id="parameterized_task",
python_callable=print_conf,
dag=dag,
)
import pendulum
from airflow import DAG
dag = DAG("example_parameterized_dag",
schedule_interval=None,
start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
catchup=False,
)
@dag.task(task_id="parameterized_task")
def print_conf(dag_run=None):
conf1 = dag_run.conf["conf1"]
print(conf1)
print_conf()

We've moved away from using PythonOperator with preference for the TaskFlow API. I don't think the docs overall reflect this (...yet) but the example DAGs were all updated with this in mind. Also with the TaskFlow API you can access context variables directly without needing kwargs. I suppose this is could be argued as a style preference but helpful to have another example in the docs IMO.

@potiuk
Copy link
Member

potiuk commented Jun 19, 2022

Static checks failing. Please fix them @patricker and rebase. Installing pre-commits helps as a lot of problems are fixed for you automatically when you commit.

dag=dag,
)

**Note**: The parameters from ``dag_run.conf`` can only be used in a template field of an operator.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s make this a normal paragraph; I feel it reads better combined with the newly added content. Something like this:

Note that the parameters from […]. Example of a parametrized DAG […]:

@patricker
Copy link
Contributor Author

I'll try and wrap this up in the next couple days, thanks for the feedback!

@github-actions
Copy link

github-actions bot commented Aug 7, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale Stale PRs per the .github/workflows/stale.yml policy file label Aug 7, 2022
@github-actions github-actions bot closed this Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:documentation stale Stale PRs per the .github/workflows/stale.yml policy file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants