Skip to content

Result of expanded task is None instead of empty list when all mapped tasks return None #48005

Description

@cedricdeboom

Apache Airflow version

2.10.5

If "Other Airflow 2 version" selected, which one?

No response

What happened?

Upon expanding a task using a list of variables, the return value of the task should be a list of all the individual return values. When the return value of one mapped task is None, it is automatically filtered out of this list. You might expect then that if all mapped tasks return None, the list is empty. However, that is not the case. Instead of returning an empty list, the return value is None. This causes issues if the output is used to expand upon in downstream tasks.

What you think should happen instead?

The return value of an expanded task should always be a list, even if all the mapped tasks have returned None (in which case the list should be empty, not None).

How to reproduce

Example dag to reproduce.

import logging
from datetime import datetime
from airflow.decorators import dag, task

logger = logging.getLogger(__name__)

@dag(schedule=None, start_date=datetime(2024, 3, 20), catchup=False, dag_id="example_dag")
def example_dag():

    @task
    def generate_data():
        # Generating a list of numbers
        return [1, 3]

    @task
    def filter_data(number):
        # Filtering only even numbers
        return number if number % 2 == 0 else None

    @task
    def display_results(results):
        logger.info(results)

    numbers = generate_data()

    filtered_numbers = filter_data.expand(number=numbers)
    display_results(filtered_numbers)

example_dag()

Operating System

MacOS Sequoia 15.3.2

Versions of Apache Airflow Providers

No response

Deployment

Virtualenv installation

Deployment details

Standalone

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions