Skip to content

DateTimeSensor() does not work with datetimes rendered via render_template_as_native_obj=True #38336

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

Closed
2 tasks done
dwreeves opened this issue Mar 20, 2024 · 2 comments · Fixed by #50744
Closed
2 tasks done

Comments

@dwreeves
Copy link
Contributor

Apache Airflow version

2.8.3

If "Other Airflow 2 version" selected, which one?

No response

What happened?

Running the following code produces an error:

with DAG(
        dag_id="test_date_time_sensor",
        schedule="@monthly",
        start_date=datetime(2024, 1, 1),
        render_template_as_native_obj=True
) as dag:

    wait = DateTimeSensorAsync(
        task_id="wait",
        target_time="{{ (data_interval_end + macros.timedelta(minutes=1)) }}"
    )

Here's the full traceback:

  File "/usr/local/lib/python3.11/site-packages/airflow/models/taskinstance.py", line 444, in _execute_task
    result = _execute_callable(context=context, **execute_callable_kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/airflow/models/taskinstance.py", line 414, in _execute_callable
    return execute_callable(context=context, **execute_callable_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/airflow/sensors/date_time.py", line 94, in execute
    trigger = DateTimeTrigger(moment=timezone.parse(self.target_time))
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/airflow/utils/timezone.py", line 213, in parse
    return pendulum.parse(string, tz=timezone or TIMEZONE, strict=strict)  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pendulum/parser.py", line 30, in parse
    return _parse(text, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pendulum/parser.py", line 43, in _parse
    parsed = base_parse(text, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pendulum/parsing/__init__.py", line 78, in parse
    return _normalize(_parse(text, **_options), **_options)
                      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pendulum/parsing/__init__.py", line 113, in _parse
    return parse_iso8601(text)
           ^^^^^^^^^^^^^^^^^^^
TypeError: argument 'input': 'DateTime' object cannot be converted to 'PyString'

The way to fix it is to add a | string filter to the Jinja:

target_time="{{ (data_interval_end + macros.timedelta(minutes=1)) | string }}"

What you think should happen instead?

The operator should handle typing for the timestamp in execute() instead of in __init__, so that runtime conversions can be properly handled.

How to reproduce

See code example above.

Operating System

Not relevant

Versions of Apache Airflow Providers

No response

Deployment

Other

Deployment details

Deployment is not relevant.

Anything else?

This can also probably be removed this the code for DateTimeSensorAsync while I/we are at it:

    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

@dwreeves dwreeves added area:core kind:bug This is a clearly a bug needs-triage label for new issues that we didn't triage yet labels Mar 20, 2024
@Taragolis Taragolis removed the needs-triage label for new issues that we didn't triage yet label Mar 20, 2024
@pierregillet
Copy link

I encountered the same error, exactly the same use case (wait some time after data_interval_end before continuing).
Thanks for the temporary fix!

@nailo2c
Copy link
Contributor

nailo2c commented May 18, 2025

I'd like to take this one on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants