Skip to content

Fix context_merge discarding empty dictionary context - #77171

Open
pranjal9091 wants to merge 1 commit into
apache:mainfrom
pranjal9091:fix/context-merge-empty-dict
Open

pranjal9091 wants to merge 1 commit into
apache:mainfrom
pranjal9091:fix/context-merge-empty-dict

Conversation

@pranjal9091

@pranjal9091 pranjal9091 commented Nov 16, 2026 •

Copy link
Copy Markdown

closes: #77137

Description

In airflow.sdk.definitions.context.context_merge:

def context_merge(context: Context, *args: Any, **kwargs: Any) -> None:
    if not context:
        context = Context()
    context.update(*args, **kwargs)

When callers pass an empty dictionary (such as context={} in unit tests following the standard operator testing pattern operator.execute(context={})), if not context: evaluates to True. Because Python passes arguments by reference, re-assigning the local variable context = Context() rebinds only the local reference in the function scope rather than mutating the caller's mapping in-place.

In PythonOperator.execute:

context_merge(context, self.op_kwargs, templates_dict=self.templates_dict)
self.op_kwargs = self.determine_kwargs(context)

Because caller's context remained {}:

  1. determine_kwargs(context) found none of the callable's arguments in context and reset self.op_kwargs = {}.
  2. When executing the callable, required arguments were missing, raising TypeError: ... missing required positional argument.

This removes the local reassignment in context_merge so context.update(*args, **kwargs) directly updates any mapping (including empty dicts) in-place.

Testing

  • Added unit tests to task-sdk/tests/task_sdk/definitions/test_context.py covering context_merge with empty dicts, kwargs, existing dictionaries, and iterable key-value pairs.
  • Ran tests via pytest; all tests passed. Ran ruff lint and format checks.

When passing an empty mapping (such as `{}` during unit testing of
custom operators with `operator.execute(context={})`), `context_merge`
evaluated `if not context: context = Context()`. Because `{}` is falsy,
this rebound the local `context` parameter to a newly created `Context()`
instance instead of mutating the passed dictionary in-place. As a result,
`op_kwargs` merged into context were discarded from the caller's perspective,
causing operators like `PythonOperator` to drop their callable keyword arguments.

This removes the local re-assignment so `context.update(*args, **kwargs)`
always updates the caller's mapping in-place.
@pranjal9091
pranjal9091 force-pushed the fix/context-merge-empty-dict branch from ccaf239 to b0745a9 Compare November 17, 2026 03:50

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PythonOperator.execute(context={}) loses 'op_kwargs' in Airflow 3.x, breaking documented custom-operator testing pattern

1 participant