frankbreetz opened a new issue, #28504:
URL: https://github.com/apache/airflow/issues/28504

   ### Apache Airflow version
   
   2.5.0
   
   ### What happened
   
   When instantiating the PythonVirtualenvOperator as part of the execute 
function of a custom Operator you must pass the DAG to it and you cannot reuse 
the task name. This is different than how it worked in Airflow Version 2.0
   
   
   ### What you think should happen instead
   
   The following DAG would parse and run:
   
   
   ```
   from datetime import datetime
   from airflow.decorators import dag, task
   from airflow.models import BaseOperator
   from airflow.operators.bash import BashOperator
   from airflow.operators.python import PythonVirtualenvOperator
   
   
   def my_func():
       print("this is a function")
   
   
   class MyOperator(BaseOperator):
       def __init__(
           self,
           **kwargs,
       ) -> None:
           super().__init__(**kwargs)
   
       def execute(self, context):
           self.operator = PythonVirtualenvOperator(
               task_id=f"{self.task_id}",
               python_callable=my_func,
               python_version="3.9",
               requirements=[f"requests"],
           )
           self.operator.execute(context)
   
   
   @dag(
       schedule=None,
       start_date=datetime(2021, 1, 1),
       catchup=False,
       default_args={
           "retries": 0,
       },
   )
   def my_dag():
       my_operator = MyOperator(task_id="my_task")
   ```
   
   ### How to reproduce
   
   Upload this dag to airflow and you will to the following error when you try 
to run it.
   
   ```
   from datetime import datetime
   from airflow.decorators import dag, task
   from airflow.models import BaseOperator
   from airflow.operators.bash import BashOperator
   from airflow.operators.python import PythonVirtualenvOperator
   
   
   def my_func():
       print("this is a function")
   
   
   class MyOperator(BaseOperator):
       def __init__(
           self,
           **kwargs,
       ) -> None:
           super().__init__(**kwargs)
   
       def execute(self, context):
           self.operator = PythonVirtualenvOperator(
               task_id=f"{self.task_id}_1",
               python_callable=my_func,
               python_version="3.9",
               requirements=[f"requests"],
              # uncomment this line to get the dag working
              # dag=context["dag"], 
           )
           # This operator works without passing the DAG in as an argument or 
updating the task name
           # self.operator = BashOperator(
           #     task_id=f"{self.task_id}",
           #     bash_command='echo Hello World!',
           # )
           self.operator.execute(context)
   
   
   @dag(
       schedule=None,
       start_date=datetime(2021, 1, 1),
       catchup=False,
       default_args={
           "retries": 0,
       },
   )
   def my_dag():
       my_operator = MyOperator(task_id="my_task")
   
   
   my_dag = my_dag()
   
   ```
   
   ### Operating System
   
   Debian GNU/Linux 11 (bullseye)
   
   ### Versions of Apache Airflow Providers
   
   _No response_
   
   ### Deployment
   
   Astronomer
   
   ### Deployment details
   
   reproduce in Astro CLI and in an Astro Deployment
   
   ### 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)
   


-- 
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]

Reply via email to