Skip to content

DAG Serialization doesn't have mro context for Operators #18734

@bhavaniravi

Description

@bhavaniravi

Apache Airflow version

2.1.3

Operating System

Debian GNU/Linux 10 (buster)

Versions of Apache Airflow Providers

No response

Deployment

Other

Deployment details

Breeze

What happened

Dag serialization loses the parent class context and MRO for the operators. This was the root cause of #18492
We cannot use isinstance(task, <OperatorClass>) after deserializing DAG

What you expected to happen

No response

How to reproduce

Example DAG

from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.utils.dates import days_ago
from datetime import datetime

DAG_NAME = 'example_subdag_operator'

args = {
    'owner': 'airflow',
}
with DAG(
    dag_id=DAG_NAME, default_args=args, start_date=days_ago(2), schedule_interval="@once", tags=['example']
) as dag:

    tn = PythonOperator(
            task_id=f'simple_python',
            python_callable=simple_python,  # make sure you don't include the () of the function
            provide_context=True,
    )

Serialization script

import datetime
from airflow.operators.dummy import DummyOperator
from airflow.models import DAG
from airflow.serialization.serialized_objects import SerializedDAG
import rich

from example_subdag import dag, PythonOperator, CustomPythonOperator

task = dag.get_task("simple_python")
print ((isinstance(task, PythonOperator)))


output_dict = SerializedDAG.to_dict(dag)
new_dag = SerializedDAG.from_dict(output_dict)


task = new_dag.get_task("simple_python")
print ((isinstance(task, PythonOperator))) # Returns false

Anything else

Related issues

#18492

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions