erdos2n opened a new issue, #30023: URL: https://github.com/apache/airflow/issues/30023
### What do you see as an issue? In the doc below, it states `Make sure to use variable with template in operator, not in the top level code.` https://github.com/apache/airflow/blob/main/docs/apache-airflow/best-practices.rst It then gives this example as a Good Example. **Good Example** ``` bash_use_variable_good = BashOperator( task_id="bash_use_variable_good", bash_command="echo variable foo=${foo_env}", env={"foo_env": "{{ var.value.get('foo') }}"}, ) ``` example below, since `{{ var.value.get('foo') }}` is in the top level code (since the `__init__` method is run every time the dag file is parsed. This can be ambiguous for users, especially new users, to understand the true difference between templated and non-templated variables. The difference between the two examples below isn't that one of them is using top-level code and the other isn't, it's that one is jinja templated and the other isn't. There is a great opportunity here to showcase the utility of jinja templating. ``` bash_use_variable_bad_3 = BashOperator( task_id="bash_use_variable_bad_3", bash_command="echo variable foo=${foo_env}", env={"foo_env": Variable.get("foo")}, # DON'T DO THAT ) ``` and ``` bash_use_variable_good = BashOperator( task_id="bash_use_variable_good", bash_command="echo variable foo=${foo_env}", env={"foo_env": "{{ var.value.get('foo') }}"}, ) ``` ### Solving the problem Replacing `Make sure to use variable with template in operator, not in the top level code.` with a sentence that is more in line with the examples following it will not only show alignment but also highlight the benefits of jinja templating in top level code. Perhaps: ``` In top-level code, variables using jinja templates do not produce a request until runtime, whereas, `Variable.get()` produces a request every time the dag file is parsed by the scheduler. This will lead to suboptimal performance for the scheduler and can cause the dag file to timeout before it is fully parsed. ``` ### Anything else _No response_ ### Are you willing to submit PR? - [X] Yes I am willing to submit a PR! ### Code of Conduct - [X] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
