Skip to content

Commit cd033e8

Browse files
committed
Explicitly cover for shared packages in exception
1 parent 03c7139 commit cd033e8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

airflow-core/src/airflow/exceptions.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,31 @@
2121

2222
from __future__ import annotations
2323

24+
import os
2425
from http import HTTPStatus
2526
from typing import TYPE_CHECKING, NamedTuple
2627

2728
if TYPE_CHECKING:
2829
from airflow.models import DagRun
2930

30-
from airflow.sdk.exceptions import AirflowException, AirflowNotFoundException
31+
# When _AIRFLOW__AS_LIBRARY is set, airflow.sdk may not be installed
32+
# In that case, we define fallback exception classes
33+
if os.environ.get("_AIRFLOW__AS_LIBRARY"):
34+
try:
35+
from airflow.sdk.exceptions import AirflowException, AirflowNotFoundException
36+
except ImportError:
37+
# Fallback exception classes when airflow.sdk is not installed
38+
class AirflowException(RuntimeError):
39+
"""Base exception for Airflow errors."""
40+
41+
pass
42+
43+
class AirflowNotFoundException(AirflowException):
44+
"""Raise when a requested object is not found."""
45+
46+
pass
47+
else:
48+
from airflow.sdk.exceptions import AirflowException, AirflowNotFoundException
3149

3250

3351
class TaskNotFound(AirflowException):

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def test_no_target_time(self):
112112
"""Check if BranchDateTimeOperator raises exception on missing target"""
113113
with pytest.raises(AirflowException):
114114
BranchDateTimeOperator(
115-
task_id="datetime_branch_2",
115+
task_id="datetime_branch",
116116
follow_task_ids_if_true="branch_1",
117117
follow_task_ids_if_false="branch_2",
118118
target_upper=None,

0 commit comments

Comments
 (0)