Skip to content

Fix Variable.get() at parse time during dag.test()#63459

Open
andreahlert wants to merge 6 commits into
apache:mainfrom
andreahlert:fix/variable-access-dag-test
Open

Fix Variable.get() at parse time during dag.test()#63459
andreahlert wants to merge 6 commits into
apache:mainfrom
andreahlert:fix/variable-access-dag-test

Conversation

@andreahlert

@andreahlert andreahlert commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

Summary

When running python my_dag.py with dag.test() at the bottom, any
Variable.get() at module level fails because SUPERVISOR_COMMS is not
yet initialized. In production the DAG processor sets it before parsing,
but the dag.test() / direct script execution flow does not.

  • Add module-level __getattr__ in task_runner.py that lazily creates
    a ParseTimeComms when SUPERVISOR_COMMS is first accessed at parse time
  • ParseTimeComms queries the metadata DB directly via SQLAlchemy,
    avoiding the Execution API HTTP layer (which would cause a recursive
    deadlock: the API route detects SUPERVISOR_COMMS and delegates back to
    the SDK in an infinite loop)
  • Server processes (scheduler, API) are guarded via _AIRFLOW_PROCESS_CONTEXT
  • Falls back gracefully when DB is not configured (settings.Session is None)

closes: #51816

Test plan

  • Unit tests for __getattr__ behavior (4 tests in test_task_runner.py)
  • Unit tests for ParseTimeComms.send() with mocked DB (4 tests in test_supervisor.py)
  • Existing test_variables.py tests pass (9 tests)
  • E2E via Breeze: DAG file with Variable.get() at top level + dag.test() resolves
    variable from DB, task executes successfully with the resolved value

Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 4.6)

Generated-by : Claude Code (Opus 4.6) following [the guidelines]

Used for helping on multiple tests on breeze.

(https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)

@andreahlert andreahlert force-pushed the fix/variable-access-dag-test branch 2 times, most recently from 3f6d92c to 8517aa0 Compare March 13, 2026 05:21
When running `python my_dag.py` with `dag.test()` at the bottom, any
`Variable.get()` at module level fails because `SUPERVISOR_COMMS` is
not yet initialized.

Add direct DB query fallback in `_get_variable` and `_get_connection`
(context.py) that fires only when no SUPERVISOR_COMMS exists and we
are not in a server process. This avoids the Execution API HTTP layer
which would cause a recursive deadlock.

closes: apache#51816

Signed-off-by: André Ahlert <andre@aex.partners>
@andreahlert andreahlert force-pushed the fix/variable-access-dag-test branch from 8517aa0 to fc79672 Compare March 16, 2026 06:45
time_machine.travel with a naive string treats it as local time,
causing logical_date mismatch in UTC-offset timezones. Use
UTC-aware datetime instead. Also fix positional vs keyword arg
mismatch in mock assertions to match actual send() call sites.

Signed-off-by: André Ahlert <andre@aex.partners>
@andreahlert andreahlert marked this pull request as ready for review March 18, 2026 20:19
@potiuk potiuk added the ready for maintainer review Set after triaging when all criteria pass. label Apr 2, 2026
…-dag-test

# Conflicts:
#	task-sdk/tests/task_sdk/execution_time/test_supervisor.py

Signed-off-by: André Ahlert <andre@aex.partners>
@potiuk

potiuk commented May 10, 2026

Copy link
Copy Markdown
Member

I am not sure if this is the right way @amoghrajesh @ashb @kaxil ?

@potiuk potiuk removed the ready for maintainer review Set after triaging when all criteria pass. label May 18, 2026
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.

ImportError: cannot import name 'SUPERVISOR_COMMS' with dag.test()

2 participants