Skip to content

Commit 4249e90

Browse files
kaxilBracken, Stephen (London)
authored andcommitted
Remove set_current_context from airflow.models.taskinstance (apache#53036)
This function isn't used anymore since Airflow 3.0. Only usage in test has been updated.
1 parent d738f92 commit 4249e90

File tree

2 files changed

+5
-24
lines changed

2 files changed

+5
-24
lines changed

airflow-core/src/airflow/models/taskinstance.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import os
2727
import uuid
2828
from collections import defaultdict
29-
from collections.abc import Collection, Generator, Iterable
29+
from collections.abc import Collection, Iterable
3030
from datetime import timedelta
3131
from functools import cache
3232
from typing import TYPE_CHECKING, Any
@@ -156,28 +156,6 @@ def _add_log(
156156
)
157157

158158

159-
@contextlib.contextmanager
160-
def set_current_context(context: Context) -> Generator[Context, None, None]:
161-
"""
162-
Set the current execution context to the provided context object.
163-
164-
This method should be called once per Task execution, before calling operator.execute.
165-
"""
166-
from airflow.sdk.definitions._internal.contextmanager import _CURRENT_CONTEXT
167-
168-
_CURRENT_CONTEXT.append(context)
169-
try:
170-
yield context
171-
finally:
172-
expected_state = _CURRENT_CONTEXT.pop()
173-
if expected_state != context:
174-
log.warning(
175-
"Current context is not equal to the state at context stack. Expected=%s, got=%s",
176-
context,
177-
expected_state,
178-
)
179-
180-
181159
def _stop_remaining_tasks(*, task_instance: TaskInstance, task_teardown_map=None, session: Session):
182160
"""
183161
Stop non-teardown tasks in dag.

providers/standard/tests/unit/standard/operators/test_python.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
DeserializingResultError,
4949
)
5050
from airflow.models.connection import Connection
51-
from airflow.models.taskinstance import TaskInstance, clear_task_instances, set_current_context
51+
from airflow.models.taskinstance import TaskInstance, clear_task_instances
5252
from airflow.providers.standard.operators.empty import EmptyOperator
5353
from airflow.providers.standard.operators.python import (
5454
BranchExternalPythonOperator,
@@ -74,8 +74,11 @@
7474

7575
if AIRFLOW_V_3_0_PLUS:
7676
from airflow.sdk import BaseOperator
77+
from airflow.sdk.execution_time.context import set_current_context
7778
else:
7879
from airflow.models.baseoperator import BaseOperator # type: ignore[no-redef]
80+
from airflow.models.taskinstance import set_current_context # type: ignore[attr-defined,no-redef]
81+
7982

8083
if TYPE_CHECKING:
8184
from airflow.models.dag import DAG

0 commit comments

Comments
 (0)