title: Return list of tasks from function that should be run in sequence in Airflow
body: <p>I want to return 2 or more tasks from a function that should be run in sequence in the spot they're inserted in the dependencies, see below.</p>
  <pre><code>t1 = PythonOperator()
  
  def generate_tasks():
      t2 = PythonOperator()
      t3 = PythonOperator()
      return magic(t2, t3) # magic needed here (preferably)
  
  t1 &gt;&gt; generate_tasks() # otherwise here
  # desired result: t1 &gt;&gt; t2 &gt;&gt; t3
  </code></pre>
  <p>Is this doable? As I understand it Airflow 2.0 seems to achieve this with a TaskGroup, but we're on Google's Composer, and 2.0 won't be available for a while.</p>
  <p>Best workaround I've found:</p>
  <pre><code>t1 = PythonOperator()
  
  def generate_tasks():
      t2 = PythonOperator()
      t3 = PythonOperator()
      return [t2, t3]
  
  tasks = generate_tasks()
  t1 &gt;&gt; tasks[0] &gt;&gt; tasks[1]
  </code></pre>
  <p>But I'd really like that to be abstracted away, as it more or less defeats the purpose of having multiple operators returned from a single function. We want it to be a single unit as far as the end user knows, even though it can be composed of 2 or more tasks.</p>
  <p>How to do it with TaskGroup in Airflow 2.0:</p>
  <pre><code>class Encryptor:
      def encrypt_and_archive(self):
          with TaskGroup(&quot;archive_and_encrypt&quot;) as section_1:
              encrypt = DummyOperator(task_id=&quot;encrypt&quot;)
              archive = BashOperator(task_id=&quot;archive&quot;, bash_command='echo 1')
              encrypt &gt;&gt; archive
          return section_1
  
  with DAG(dag_id=&quot;example_return_task_group&quot;, start_date=days_ago(2), tags=[&quot;example&quot;]) as dag:
      start = DummyOperator(task_id=&quot;start&quot;)
      encrypt_and_archive = Encryptor().encrypt_and_archive()
      end = DummyOperator(task_id='end')
  
               #  single variable, containing two tasks
      start &gt;&gt; encrypt_and_archive &gt;&gt; end
  </code></pre>
  <p>Which creates the following graph:</p>
  <p><a href="https://i.stack.imgur.com/cxoec.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cxoec.png" alt="TaskGroup in a graph" /></a></p>
  <p>Is something similar remotely doable before 2.0?</p>