Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose job_variables in runtime.flow_run #16124

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/prefect/runtime/flow_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"run_count",
"api_url",
"ui_url",
"job_variables",
]


Expand Down Expand Up @@ -317,6 +318,11 @@ def get_flow_run_ui_url() -> Optional[str]:
return f"{PREFECT_UI_URL.value()}/flow-runs/flow-run/{flow_run_id}"


def get_job_variables() -> Optional[Dict[str, Any]]:
flow_run_ctx = FlowRunContext.get()
return flow_run_ctx.flow_run.job_variables if flow_run_ctx else None


FIELDS = {
"id": get_id,
"tags": get_tags,
Expand All @@ -331,4 +337,5 @@ def get_flow_run_ui_url() -> Optional[str]:
"api_url": get_flow_run_api_url,
"ui_url": get_flow_run_ui_url,
"flow_version": get_flow_version,
"job_variables": get_job_variables,
}
18 changes: 18 additions & 0 deletions tests/runtime/test_flow_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,21 @@ async def test_url_pulls_from_api_when_needed(
monkeypatch.setenv(name="PREFECT__FLOW_RUN_ID", value=str(run.id))

assert getattr(flow_run, url_type) == expected_url


class TestJobVariables:
async def test_job_variables_is_attribute(self):
assert "job_variables" in dir(flow_run)

async def test_job_variables_is_none_when_not_set(self):
assert flow_run.job_variables is None

async def test_job_variables_returns_variables_when_present_dynamically(self):
assert flow_run.job_variables is None

with FlowRunContext.model_construct(
flow_run=FlowRun.model_construct(job_variables={"foo": "bar"})
):
assert flow_run.job_variables == {"foo": "bar"}

assert flow_run.job_variables is None
Loading