Repository: apache/airflow

Issue:
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'datetime.timedelta'
### Apache Airflow version

2.2.0 (latest released)

### Operating System

Debian

### Versions of Apache Airflow Providers

_No response_

### Deployment

Other Docker-based deployment

### Deployment details

_No response_

### What happened

I've upgraded to Airflow 2.2.0 and added a new operator to the existing DAG:

```
delay_sensor = TimeDeltaSensorAsync(task_id="wait", delta=timedelta(hours=24))
```

I've started getting an error:

```
[2021-10-21, 20:01:17 UTC] {taskinstance.py:1686} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1324, in _run_raw_task
    self._execute_task_with_callbacks(context)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1443, in _execute_task_with_callbacks
    result = self._execute_task(context, self.task)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1499, in _execute_task
    result = execute_callable(context=context)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/sensors/time_delta.py", line 54, in execute
    target_dttm += self.delta
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'datetime.timedelta'
[2021-10-21, 20:01:17 UTC] {taskinstance.py:1270} INFO - Marking task as UP_FOR_RETRY. dag_id=integration-ups-mpos-2215, task_id=wait, execution_date=20211020T070000, start_date=20211021T200117, end_date=20211021T200117
[2021-10-21, 20:01:17 UTC] {standard_task_runner.py:88} ERROR - Failed to execute job 1458 for task wait
Traceback (most recent call last):
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/task/task_runner/standard_task_runner.py", line 85, in _start_by_fork
    args.func(args, dag=self.dag)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/cli/cli_parser.py", line 48, in command
    return func(*args, **kwargs)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/cli.py", line 92, in wrapper
    return f(*args, **kwargs)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/cli/commands/task_command.py", line 292, in task_run
    _run_task_by_selected_method(args, dag, ti)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/cli/commands/task_command.py", line 107, in _run_task_by_selected_method
    _run_raw_task(args, ti)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/cli/commands/task_command.py", line 180, in _run_raw_task
    ti._run_raw_task(
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/session.py", line 70, in wrapper
    return func(*args, session=session, **kwargs)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1324, in _run_raw_task
    self._execute_task_with_callbacks(context)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1443, in _execute_task_with_callbacks
    result = self._execute_task(context, self.task)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1499, in _execute_task
    result = execute_callable(context=context)
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/sensors/time_delta.py", line 54, in execute
    target_dttm += self.delta
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'datetime.timedelta'
```

It looks like 2 new fields that were introduced in "dag_run" table:

* data_interval_start
* data_interval_end

I see that these fields are populated correctly for new DAG-s but for the old DAG-s the data is not back-filled.

![image](https://user-images.githubusercontent.com/131281/138353823-aaede2dc-47fd-4caf-a519-ff758ead8943.png)


### What you expected to happen

The `TimeDeltaSensorAsync` sensor should work correctly even in older DAG-s.

### How to reproduce

_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)



Discussion:
Thanks for opening your first issue here! Be sure to follow the issue template!


Resolving patch:
diff --git a/airflow/models/taskinstance.py b/airflow/models/taskinstance.py
--- a/airflow/models/taskinstance.py
+++ b/airflow/models/taskinstance.py
@@ -1776,6 +1776,7 @@ def get_template_context(self, session: Session = None, ignore_param_exceptions:
         integrate_macros_plugins()
 
         dag_run = self.get_dagrun(session)
+        data_interval = dag.get_run_data_interval(dag_run)
 
         params = ParamsDict(suppress_exception=ignore_param_exceptions)
 
@@ -1996,8 +1997,8 @@ def get_prev_ds_nodash() -> Optional[str]:
             'conf': conf,
             'dag': dag,
             'dag_run': dag_run,
-            'data_interval_end': timezone.coerce_datetime(dag_run.data_interval_end),
-            'data_interval_start': timezone.coerce_datetime(dag_run.data_interval_start),
+            'data_interval_end': timezone.coerce_datetime(data_interval.end),
+            'data_interval_start': timezone.coerce_datetime(data_interval.start),
             'ds': ds,
             'ds_nodash': ds_nodash,
             'execution_date': deprecated_proxy(


Test patch: