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

   Closes: #72031
   
   ### Problem
   
   `BranchDayOfWeekOperator` and `BranchDateTimeOperator` crash with a bare
   `AttributeError: 'NoneType' object has no attribute 'run_after'` when they 
run
   with `use_task_logical_date=True` in a context that contains neither
   `logical_date` nor `dag_run` — for example standalone task execution,
   `dag.test()`, or unit tests calling `choose_branch` directly.
   
   ### Root cause
   
   In both operators' `choose_branch`, when `logical_date` was missing the code
   fell back to `dag_run.run_after` without checking that `dag_run` exists:
   
   ```python
   now = context.get("logical_date")
   if not now:
       dag_run = context.get("dag_run")
       now = dag_run.run_after  # crashes when dag_run is None
   ```
   
   ### Fix
   
   Mirror the defensive pattern already used by `DayOfWeekSensor` in the same
   provider (`sensors/weekday.py`): resolve `logical_date` and `dag_run` up
   front, and raise a `ValueError` — "Either `logical_date` or `run_after`
   should be provided in the task context when `use_task_logical_date` is
   True" — when neither a logical date nor a DagRun `run_after` can be
   determined. When a DagRun is present, behaviour is unchanged (the existing
   `run_after` fallback tests still pass).
   
   ### Tests
   
   Added `test_choose_branch_without_logical_date_or_dag_run` to both operator
   test modules, parametrized over an empty context and a context with explicit
   `None` values. The new tests fail on the previous code with the reported
   `AttributeError` and pass with this change.
   
   Both full modules run locally (`test_weekday.py` + `test_datetime.py`):
   60 passed.
   
   ---
   ##### Was generative AI tooling used to co-author this PR?
   
   - [x] Yes (please specify the tool below)
   
   Generated-by: Muse Spark 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