pranjal9091 opened a new pull request, #77171:
URL: https://github.com/apache/airflow/pull/77171

   closes: #77137
   
   ### Description
   In `airflow.sdk.definitions.context.context_merge`:
   ```python
   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 name `context = 
Context()` rebinds only the local variable rather than mutating the caller's 
dictionary in-place.
   
   In `PythonOperator.execute`:
   ```python
   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 mutates 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, and existing dictionaries.
   - Added regression test `test_python_operator_execute_empty_context` to 
`providers/standard/tests/unit/standard/operators/test_python.py` verifying 
that `PythonOperator.execute(context={})` retains `op_kwargs` and passes them 
to the callable.
   - Ran tests via pytest; all tests passed. Ran ruff lint and format checks.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes
   
   Generated-by: AI assistant for code exploration and test structuring, 
following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)


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