nailo2c opened a new pull request, #50744:
URL: https://github.com/apache/airflow/pull/50744

   related: #38336
   
   ### 🐞 Problem
   
   This is quite an interesting case.
   
   + With `render_template_as_native_obj=True`, the Jinja expression is stored 
as a **raw string** when `DateTimeSensorAsync` is instantiated
   `{{ (data_interval_end + macros.timedelta(minutes=1)) }}` → falls through 
[line 
91](https://github.com/apache/airflow/blob/main/providers/standard/src/airflow/providers/standard/sensors/date_time.py#L91)
 and is kept as‑is.
   + At run‑time the template is rendered to a `pendulum.DateTime` object.
   When `execute()` calls
     ```python
     moment = timezone.parse(self.target_time)
     ```
     `timezone.parse` expects a string and raises `TypeError`.
   
   ### ✅ Fix
   
   + Add a private helper `_moment()` that
     + returns `self.target_time` directly if it is already a `datetime` / 
`pendulum.DateTime`;
     + otherwise parses the string with `timezone.parse`.
   + Replace all direct uses of `timezone.parse(self.target_time)` with 
`_moment()`.
   + No behaviour change for DAGs that keep 
`render_template_as_native_obj=False`.
   
   ### 📝Example DAG (runs successfully after this patch)
   
   ```python
   from datetime import datetime
   from airflow import DAG
   from airflow.providers.standard.sensors.date_time import DateTimeSensorAsync
   
   with DAG(
       dag_id="test_date_time_sensor",
       start_date=datetime(2025, 1, 1),
       schedule=None,
       render_template_as_native_obj=True,
   ):
       DateTimeSensorAsync(
           task_id="wait",
           target_time="{{ (data_interval_end + macros.timedelta(minutes=1)) 
}}",
       )
   ```
   
   ![截圖 2025-05-17 下午9 37 
11](https://github.com/user-attachments/assets/9fb54a13-6f5a-4f77-90a2-14a28772ff04)
   


-- 
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]

Reply via email to