You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "raphaelauv (via GitHub)" <gi...@apache.org> on 2023/08/08 17:17:49 UTC
[GitHub] [airflow] raphaelauv opened a new issue, #33217: get_current_context not present in user_defined_macros
raphaelauv opened a new issue, #33217:
URL: https://github.com/apache/airflow/issues/33217
### Apache Airflow version
2.6.3
### What happened
get_current_context() fail in a user_defined_macros
give
```
{abstractoperator.py:594} ERROR - Exception rendering Jinja template for task 'toot', field 'op_kwargs'. Template: {'arg': '{{ macro_run_id() }}'}
Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/abstractoperator.py", line 586, in _do_render_template_fields
rendered_content = self.render_template(
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/template/templater.py", line 168, in render_template
return {k: self.render_template(v, context, jinja_env, oids) for k, v in value.items()}
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/template/templater.py", line 168, in <dictcomp>
return {k: self.render_template(v, context, jinja_env, oids) for k, v in value.items()}
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/template/templater.py", line 156, in render_template
return self._render(template, context)
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/abstractoperator.py", line 540, in _render
return super()._render(template, context, dag=dag)
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/template/templater.py", line 113, in _render
return render_template_to_string(template, context)
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/helpers.py", line 288, in render_template_to_string
return render_template(template, cast(MutableMapping[str, Any], context), native=False)
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/helpers.py", line 283, in render_template
return "".join(nodes)
File "<template>", line 12, in root
File "/home/airflow/.local/lib/python3.8/site-packages/jinja2/sandbox.py", line 393, in call
return __context.call(__obj, *args, **kwargs)
File "/home/airflow/.local/lib/python3.8/site-packages/jinja2/runtime.py", line 298, in call
return __obj(*args, **kwargs)
File "/opt/airflow/dags/dags/exporter/finance_closing.py", line 7, in macro_run_id
schedule_interval = get_current_context()["dag"].schedule_interval.replace("@", "")
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/operators/python.py", line 784, in get_current_context
raise AirflowException(
airflow.exceptions.AirflowException: Current context was requested but no context was found! Are you running within an airflow task?
```
### What you think should happen instead
User macros should be able to access to the current context
```
airflow.exceptions.AirflowException: Current context was requested but no context was found! Are you running within an airflow task?
```
### How to reproduce
```python
from airflow.models import DAG
from airflow.operators.python import PythonOperator
from airflow.utils.dates import days_ago
def macro_run_id():
from airflow.operators.python import get_current_context
schedule_interval = get_current_context()["dag"].schedule_interval.replace("@", "")
if schedule_interval == "None":
schedule_interval = "manual"
return schedule_interval
with DAG(dag_id="example2",
start_date=days_ago(61),
user_defined_macros={"macro_run_id": macro_run_id},
schedule_interval="@monthly"):
def toto(arg):
print(arg)
PythonOperator(task_id="toot", python_callable=toto,
op_kwargs={"arg": "{{ macro_run_id() }}"})
```
### Operating System
ubuntu 22.04
### Versions of Apache Airflow Providers
_No response_
### Deployment
Other
### Deployment details
_No response_
### Anything else
_No response_
### Are you willing to submit PR?
- [ ] 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: commits-unsubscribe@airflow.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] get_current_context not present in user_defined_macros [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on issue #33217:
URL: https://github.com/apache/airflow/issues/33217#issuecomment-1871819590
> @ivan-afonichkin I found that this issue was fixed at 2.7.1 version.we may not upgrade airflow now , so is there a way to fix this issue under 2.7.1 version?
You can attempt to patch it manually - source of airflow is fully available and you might want to patch your source. But I heartily recommend to just upgrade airflow. Every single airflow release contains 10s of fixes to known problems in previous versions, a lot of them contains fixes to security issues and generally - almost by defintion - when you do not run latest version, you run a version that contains known bugs and security fixes. So "upgrade airflow to latest version" is abaolutely best way that you should proceed.
You have to remember that even if it costs some time to upgrade, it will likely cost you (and your organisation) a lot more to NOT upgrade - because of known bugs that are fixed and potential risk of security issues you have not applied (and in the future when acts like CRA (Cyber Resilience Act ) in EU and similar in the US will be approved and become the law (it will be passed in EU in a month or two) your organisation will have to upgrade to latest security-fixed version of software anyway and it will cost penalties if they don't, so I suggest get used to situation that you upgrade software as soon as it is released.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] ivan-afonichkin commented on issue #33217: get_current_context not present in user_defined_macros
Posted by "ivan-afonichkin (via GitHub)" <gi...@apache.org>.
ivan-afonichkin commented on issue #33217:
URL: https://github.com/apache/airflow/issues/33217#issuecomment-1672633607
Hello @potiuk @raphaelauv, I would like to take this issue. Will submit PR in a few days. I'm not sure how I can assign it to myself though.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] eladkal commented on issue #33217: get_current_context not present in user_defined_macros
Posted by "eladkal (via GitHub)" <gi...@apache.org>.
eladkal commented on issue #33217:
URL: https://github.com/apache/airflow/issues/33217#issuecomment-1672668671
> Hello @potiuk @raphaelauv, I would like to take this issue. Will submit PR in a few days. I'm not sure how I can assign it to myself though.
assigned
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] josh-fell commented on issue #33217: get_current_context not present in user_defined_macros
Posted by "josh-fell (via GitHub)" <gi...@apache.org>.
josh-fell commented on issue #33217:
URL: https://github.com/apache/airflow/issues/33217#issuecomment-1679321246
FWIW, this can be used as a workaround:
```py
from airflow.models import DAG
from airflow.operators.python import PythonOperator
from airflow.utils.dates import days_ago
def macro_run_id(dag):
a = dag.schedule_interval.replace("@", "")
if a == "None":
a = "manual"
return a
with DAG(
dag_id="example2",
start_date=days_ago(61),
user_defined_macros={"macro_run_id": macro_run_id},
schedule_interval="@monthly",
):
def toto(arg):
print(arg)
PythonOperator(task_id="toot", python_callable=toto, op_kwargs={"arg": "{{ macro_run_id(dag) }}"})
```
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] get_current_context not present in user_defined_macros [airflow]
Posted by "messi-gao (via GitHub)" <gi...@apache.org>.
messi-gao commented on issue #33217:
URL: https://github.com/apache/airflow/issues/33217#issuecomment-1871690456
@ivan-afonichkin I found that this issue was fixed at 2.7.1 version.is there a way to fix this issue under 2.7.1 version?
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] raphaelauv commented on issue #33217: get_current_context not present in user_defined_macros
Posted by "raphaelauv (via GitHub)" <gi...@apache.org>.
raphaelauv commented on issue #33217:
URL: https://github.com/apache/airflow/issues/33217#issuecomment-1679409735
Thanks @josh-fell , it's a great hack for my (specific) use case 👍👍👍
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] potiuk closed issue #33217: get_current_context not present in user_defined_macros
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk closed issue #33217: get_current_context not present in user_defined_macros
URL: https://github.com/apache/airflow/issues/33217
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org