josh-fell commented on issue #17022:
URL: https://github.com/apache/airflow/issues/17022#issuecomment-887959994
FWIW this is also reproducible in Airflow 2.
**Airflow version**
2.1.0
Using the DAG below, the `get_previous_execution_date()` call from
`TaskInstance` object directly succeeds while the same method call accessing
the `TaskInstance` from the `DagRun` object fails for the same exception noted
originally.
```python
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.utils.dates import days_ago
dag = DAG("mwe", start_date=days_ago(14))
def some_function(dag_run, ti):
from airflow.utils.state import State
dag_run_tis = dag_run.get_task_instances()
print(ti.get_previous_execution_date(state=State.SUCCESS))
print(dag_run_tis[0].get_previous_execution_date(state=State.SUCCESS))
with dag:
op = PythonOperator(task_id="op_some_function",
python_callable=some_function)
```
It appears as though the the `TaskInstance` objects return different sets of
attributes when accessing them directly via the "ti" context or the "dag_run"
context and indeed, there is no `task` attribute in the `TaskInstance` returned
via `DagRun`.
Get Previous Execution Date from TaskInstance | Get Previous Execution Date
via DagRun |
----------------------------------------------- |
----------------------------------------- |
`ti.get_previous_execution_date()` |
`dag_run.get_task_instances()[0].get_previous_execution_date()` |
_log | _sa_instance_state |
_sa_instance_state | _try_number |
_try_number | dag_id |
dag_id | duration |
duration | end_date |
end_date | execution_date |
execution_date | executor_config |
executor_config | external_executor_id |
hostname | hostname |
job_id | job_id |
max_tries | max_tries |
operator | operator |
pid | pid |
pool | pool |
pool_slots | pool_slots |
priority_weight | priority_weight |
queue | queue |
queued_dttm | queued_by_job_id |
raw | queued_dttm |
run_as_user | start_date |
start_date | state |
state | task_id |
task | test_mode |
task_id | unixname |
test_mode |
unixname |
@tsabsch Have you tried directly accessing the `TaskInstance` rather than
going through `DagRun` as a workaround?
--
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]