forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AIP-72: Add support for
get_current_context
in Task SDK (apache#45486)
closes apache#45234 I am putting the logic for `set_current_context` in `execution_time/context.py`. I didn't want to put `_CURRENT_CONTEXT` in `task_sdk/src/airflow/sdk/definitions/contextmanager.py` to avoid execution logic in a user-facing module but I couldn't think of another way to store it from execution & allow retrieving (via `get_current_context` in the Standard Provider) in their Task. Upcoming PRs: - Move most of the internal stuff in Task SDK to a separate module. - Use `create_runtime_ti` fixture more widely in tests --- Tested with the following DAG: ```py import pendulum from airflow.decorators import dag, task from airflow.providers.standard.operators.python import get_current_context @dag( schedule=None, start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), catchup=False, ) def x_get_context(): @task def template_test(data_interval_end): context = get_current_context() # Will print `2024-10-10 00:00:00+00:00`. # Note how we didn't pass this value when calling the task. Instead # it was passed by the decorator from the context print(f"data_interval_end: {data_interval_end}") # Will print the full context dict print(f"context: {context}") template_test() x_get_context() ``` <img width="1703" alt="image" src="https://github.com/user-attachments/assets/2763963a-d299-412f-bee3-3b20904ca7c8" />
- Loading branch information
Showing
11 changed files
with
338 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from airflow.sdk import get_current_context | ||
|
||
|
||
class TestCurrentContext: | ||
def test_current_context_no_context_raise(self): | ||
with pytest.raises(RuntimeError): | ||
get_current_context() | ||
|
||
def test_get_current_context_with_context(self, monkeypatch): | ||
mock_context = {"ti": "task_instance", "key": "value"} | ||
monkeypatch.setattr("airflow.sdk.definitions.contextmanager._CURRENT_CONTEXT", [mock_context]) | ||
result = get_current_context() | ||
assert result == mock_context | ||
|
||
def test_get_current_context_without_context(self, monkeypatch): | ||
monkeypatch.setattr("airflow.sdk.definitions.contextmanager._CURRENT_CONTEXT", []) | ||
with pytest.raises(RuntimeError, match="Current context was requested but no context was found!"): | ||
get_current_context() |
Oops, something went wrong.