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

   ## Summary
   
   Fixes #51816
   
   `SUPERVISOR_COMMS` was declared as a bare type annotation 
(`SUPERVISOR_COMMS: CommsDecoder[...]`) in `task_runner.py`, which does **not** 
create an actual module attribute in Python. This caused `ImportError` when 
`Variable.get()` was called at the top level of a DAG file (outside task 
execution context), such as during `dag.test()`.
   
   ### Changes
   
   - **Initialize `SUPERVISOR_COMMS` to `None`** so the import always succeeds. 
The existing check in `ensure_secrets_backend_loaded()` already handles `is not 
None`, so the fallback logic is unaffected.
   - **Add `None` guards** before `.send()` calls in `_set_variable()`, 
`_delete_variable()`, and all `ExecutionAPISecretsBackend` methods to prevent 
`AttributeError` on `None`.
   - **Improve error messages** when `Variable.get()` fails outside task 
context, suggesting alternatives (environment variables, Jinja templates, 
moving the call inside a task).
   - **Add tests** for `Variable.get/set/delete` outside task execution context.
   
   ### Files changed
   
   | File | Change |
   |------|--------|
   | `task_runner.py` | `SUPERVISOR_COMMS = None` (was bare annotation) |
   | `context.py` | None guards + enhanced error messages |
   | `execution_api.py` | Defensive `return None` guards |
   | `test_variables.py` | 4 new tests for outside-task-context behavior |
   
   ## Test plan
   
   - [ ] Existing `test_variables.py` tests pass (no regression)
   - [ ] New `TestVariableOutsideTaskContext` tests pass
   - [ ] `Variable.get()` with env var works outside task context
   - [ ] `Variable.get()` without env var raises helpful error outside task 
context
   - [ ] `Variable.set()` raises clear error outside task context
   - [ ] `Variable.delete()` raises clear error outside task context


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