Skip to content
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

Ensure DateTimeTrigger receives a datetime object #17747

Merged
merged 1 commit into from
Aug 20, 2021

Conversation

kaxil
Copy link
Member

@kaxil kaxil commented Aug 20, 2021

While using the following example DAG, the task failed with moment does not have tzinfo attribute. This happened because a string was passed from DateTimeSensor to DateTimeTrigger. This PR ensures that a datetime object is passed and fixed logic in DateTimeSensor too so that self.target_time is always a datetime object.

Stacktrace:

[2021-08-19, 21:57:17 UTC] {taskinstance.py:1657} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/home/airflow/.local/lib/python3.7/site-packages/airflow/models/taskinstance.py", line 1296, in _run_raw_task
    self._prepare_and_execute_task_with_callbacks(context, task)
  File "/home/airflow/.local/lib/python3.7/site-packages/airflow/models/taskinstance.py", line 1415, in _prepare_and_execute_task_with_callbacks
    result = self._execute_task(context, task_copy)
  File "/home/airflow/.local/lib/python3.7/site-packages/airflow/models/taskinstance.py", line 1471, in _execute_task
    result = execute_callable(context=context)
  File "/home/airflow/.local/lib/python3.7/site-packages/airflow/sensors/date_time.py", line 87, in execute
    self.defer(trigger=DateTimeTrigger(moment=self.target_time), method_name="execute_complete")
  File "/home/airflow/.local/lib/python3.7/site-packages/airflow/triggers/temporal.py", line 37, in __init__
    if moment.tzinfo is None:
AttributeError: 'str' object has no attribute 'tzinfo'

Example DAG:

from datetime import timedelta

from airflow import DAG
from airflow.sensors.date_time import DateTimeSensorAsync
from airflow.utils import dates, timezone

with DAG(
    dag_id='example_date_time_async_operator',
    schedule_interval='0 0 * * *',
    start_date=dates.days_ago(2),
    dagrun_timeout=timedelta(minutes=60),
    tags=['example', 'example2', 'async'],
) as dag:

    DateTimeSensorAsync(task_id="test", target_time=timezone.datetime(2021, 8, 19, 23, 15, 0))

cc @andrewgodwin


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.

@boring-cyborg boring-cyborg bot added the area:core-operators Operators, Sensors and hooks within Core Airflow label Aug 20, 2021
@kaxil kaxil requested a review from uranusjr August 20, 2021 01:43
Copy link
Contributor

@andrewgodwin andrewgodwin left a comment

Choose a reason for hiding this comment

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

Can't believe I missed that str inputs were allowed!

Copy link
Member

@uranusjr uranusjr left a comment

Choose a reason for hiding this comment

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

Come on CI…

@github-actions github-actions bot added the full tests needed We need to run full set of tests for this PR to merge label Aug 20, 2021
@github-actions
Copy link

The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest main at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.

@kaxil kaxil closed this Aug 20, 2021
@kaxil kaxil reopened this Aug 20, 2021
@kaxil kaxil removed the full tests needed We need to run full set of tests for this PR to merge label Aug 20, 2021
While using the following example DAG, the task failed with `moment` does not have `tzinfo` attribute. This happened because a string was passed from `DateTimeSensor` to `DateTimeTrigger`. This PR ensures that a datetime object is passed and fixed logic in `DateTimeSensor` too so that `self.target_time` is always a datetime object.

```python
from datetime import timedelta

from airflow import DAG
from airflow.sensors.date_time import DateTimeSensorAsync
from airflow.utils import dates, timezone

with DAG(
    dag_id='example_date_time_async_operator',
    schedule_interval='0 0 * * *',
    start_date=dates.days_ago(2),
    dagrun_timeout=timedelta(minutes=60),
    tags=['example', 'example2', 'async'],
) as dag:

    DateTimeSensorAsync(task_id="test", target_time=timezone.datetime(2021, 8, 19, 23, 15, 0))

```
@kaxil kaxil merged commit e21b54a into apache:main Aug 20, 2021
@kaxil kaxil deleted the fix-date-time-trigger branch August 20, 2021 18:42
kaxil added a commit to astronomer/airflow that referenced this pull request Sep 1, 2021
While fixing ``DateTimeSensorAsync`` in apache#17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`
kaxil added a commit that referenced this pull request Sep 1, 2021
While fixing ``DateTimeSensorAsync`` in #17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Mar 10, 2022
While fixing ``DateTimeSensorAsync`` in apache/airflow#17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`

GitOrigin-RevId: fbdb6882e4789c55c751b8be466eed89945d3241
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Jun 4, 2022
While fixing ``DateTimeSensorAsync`` in apache/airflow#17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`

GitOrigin-RevId: fbdb6882e4789c55c751b8be466eed89945d3241
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Jul 10, 2022
While fixing ``DateTimeSensorAsync`` in apache/airflow#17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`

GitOrigin-RevId: fbdb6882e4789c55c751b8be466eed89945d3241
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Aug 27, 2022
While fixing ``DateTimeSensorAsync`` in apache/airflow#17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`

GitOrigin-RevId: fbdb6882e4789c55c751b8be466eed89945d3241
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Oct 4, 2022
While fixing ``DateTimeSensorAsync`` in apache/airflow#17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`

GitOrigin-RevId: fbdb6882e4789c55c751b8be466eed89945d3241
aglipska pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Oct 7, 2022
While fixing ``DateTimeSensorAsync`` in apache/airflow#17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`

GitOrigin-RevId: fbdb6882e4789c55c751b8be466eed89945d3241
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Dec 7, 2022
While fixing ``DateTimeSensorAsync`` in apache/airflow#17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`

GitOrigin-RevId: fbdb6882e4789c55c751b8be466eed89945d3241
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Jan 27, 2023
While fixing ``DateTimeSensorAsync`` in apache/airflow#17747 -- I broke ``DateTimeSensor``.

As `target_time` is a template_field for `DateTimeSensor`, Jinja tries to render it which does not work if the input is a datetime object or if someone passes just a template field like ``{{ execution_date }} `` it throws an error:

```
    DateTimeSensor(task_id="foo",
                   target_time="{{ execution_time }}"
    )
    '
        Traceback (most recent call last):
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 131, in _parse
            dt = parser.parse(
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 1368, in parse
            return DEFAULTPARSER.parse(timestr, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py", line 643, in parse
            raise ParserError("Unknown string format: %s", timestr)
        dateutil.parser._parser.ParserError: Unknown string format: {{ execution_time }}

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<string>", line 3, in <module>
          File "/usr/local/lib/python3.9/site-packages/airflow/models/baseoperator.py", line 186, in apply_defaults
            result = func(self, *args, **kwargs)
          File "/usr/local/lib/python3.9/site-packages/airflow/sensors/date_time.py", line 66, in __init__
            self.target_time = timezone.parse(target_time)
          File "/usr/local/lib/python3.9/site-packages/airflow/utils/timezone.py", line 175, in parse
            return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False)  # type: ignore
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 29, in parse
            return _parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parser.py", line 45, in _parse
            parsed = base_parse(text, **options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 74, in parse
            return _normalize(_parse(text, **_options), **_options)
          File "/usr/local/lib/python3.9/site-packages/pendulum/parsing/__init__.py", line 135, in _parse
            raise ParserError("Invalid date string: {}".format(text))
        pendulum.parsing.exceptions.ParserError: Invalid date string: {{ execution_time }}
```

This PR fixes it by reverting change in `DateTimeSensor` and parses the string to datetime in `DateTimeSensorAsync.execute`

GitOrigin-RevId: fbdb6882e4789c55c751b8be466eed89945d3241
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:core-operators Operators, Sensors and hooks within Core Airflow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants