Skip to content

Mapped custom operators lose overridden BaseOperator defaults during unmap #72588

Closed

Description

@eitan-shalev

Problem

A custom operator can override a BaseOperator constructor default for direct task construction, but the overridden default is lost when the same operator is dynamically mapped.

BaseOperator.partial() fills missing values in partial_kwargs from the hard-coded OPERATOR_DEFAULTS mapping. MappedOperator.unmap() then passes those values explicitly to the custom operator constructor. A base-class default therefore overrides the custom operator's Python default.

This makes a task behave differently when it is mapped, even when no mapped argument or DAG default touches the property.

Reproducer

Tested with Apache Airflow 3.3.1.

from airflow.sdk import BaseOperator, DAG, ExceptionRetryPolicy

CUSTOM_POLICY = ExceptionRetryPolicy(rules=[])


class CustomRetryOperator(BaseOperator):
    def __init__(self, *, value: str, retry_policy=CUSTOM_POLICY, **kwargs):
        super().__init__(retry_policy=retry_policy, **kwargs)
        self.value = value

    def execute(self, context):
        pass


with DAG(dag_id="mapped_custom_default", schedule=None):
    direct = CustomRetryOperator(task_id="direct", value="direct")
    mapped = CustomRetryOperator.partial(task_id="mapped").expand(value=["mapped"])

unmapped = mapped.unmap({"value": "mapped"})

assert direct.retry_policy is CUSTOM_POLICY
assert mapped.partial_kwargs["retry_policy"] is None
assert unmapped.retry_policy is CUSTOM_POLICY  # Fails: actual value is None

The same pattern should affect any property whose subclass default differs from the corresponding entry in OPERATOR_DEFAULTS, not only retry_policy.

Expected behavior

If a property is not explicitly supplied to .partial(), .expand(), or DAG/task default_args, an unmapped task instance should receive the same custom-operator default as a directly constructed task.

Explicit partial arguments and DAG/task defaults should keep their current precedence.

Suggested direction

When constructing partial_kwargs, resolve absent defaults from the effective operator_class.__init__ signature for parameters declared by that class, instead of unconditionally materializing the BaseOperator value from OPERATOR_DEFAULTS.

An alternative with a smaller compatibility surface would be a documented class hook for custom operators to provide mapping-time defaults. In either approach, add a regression test for a custom operator that overrides a BaseOperator field default and assert that direct and mapped/unmapped construction produce the same value.

Relevant implementation

  • task-sdk/src/airflow/sdk/bases/operator.py: partial() fills OPERATOR_DEFAULTS into partial_kwargs.
  • task-sdk/src/airflow/sdk/definitions/mappedoperator.py: unmap() re-instantiates the custom operator from those kwargs.

Activity

  1. webdevsamran commented on Sep 6, 2026

    @webdevsamran

    Hi team, I would like to work on this issue.

    In BaseOperator.partial() and MappedOperator.unmap(), injecting hardcoded OPERATOR_DEFAULTS into partial_kwargs causes custom operator subclasses that override base parameter defaults to have their custom defaults overwritten during unmapping. I can update the unmapping / partial argument resolution logic to respect subclass constructor defaults, and add regression tests for dynamically mapped custom operators.

    Could you please assign this to me? Thanks!

  2. eitan-shalev commented on Sep 6, 2026

    @eitan-shalev
    ContributorAuthor

    Hi @webdevsamran , I created a PR for this, and I'd love your opinion on the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions