Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/airflow/providers/common/compat/sdk.py: 45%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

31 statements  

1# Licensed to the Apache Software Foundation (ASF) under one 

2# or more contributor license agreements. See the NOTICE file 

3# distributed with this work for additional information 

4# regarding copyright ownership. The ASF licenses this file 

5# to you under the Apache License, Version 2.0 (the 

6# "License"); you may not use this file except in compliance 

7# with the License. You may obtain a copy of the License at 

8# 

9# http://www.apache.org/licenses/LICENSE-2.0 

10# 

11# Unless required by applicable law or agreed to in writing, 

12# software distributed under the License is distributed on an 

13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 

14# KIND, either express or implied. See the License for the 

15# specific language governing permissions and limitations 

16# under the License. 

17""" 

18Airflow compatibility imports for seamless migration from Airflow 2 to Airflow 3. 

19 

20This module provides lazy imports that automatically try Airflow 3 paths first, 

21then fall back to Airflow 2 paths, enabling code to work across both versions. 

22""" 

23 

24from __future__ import annotations 

25 

26from typing import TYPE_CHECKING 

27 

28from airflow.providers.common.compat.version_compat import AIRFLOW_V_3_0_PLUS 

29 

30if TYPE_CHECKING: 

31 import airflow.sdk.io as io # noqa: F401 

32 import airflow.sdk.timezone as timezone # noqa: F401 

33 from airflow.models.xcom import XCOM_RETURN_KEY as XCOM_RETURN_KEY 

34 from airflow.sdk import ( 

35 DAG as DAG, 

36 Asset as Asset, 

37 AssetAlias as AssetAlias, 

38 AssetAll as AssetAll, 

39 AssetAny as AssetAny, 

40 BaseHook as BaseHook, 

41 BaseNotifier as BaseNotifier, 

42 BaseOperator as BaseOperator, 

43 BaseOperatorLink as BaseOperatorLink, 

44 BaseSensorOperator as BaseSensorOperator, 

45 Connection as Connection, 

46 Context as Context, 

47 DagRunState as DagRunState, 

48 EdgeModifier as EdgeModifier, 

49 Label as Label, 

50 Metadata as Metadata, 

51 ObjectStoragePath as ObjectStoragePath, 

52 Param as Param, 

53 PokeReturnValue as PokeReturnValue, 

54 TaskGroup as TaskGroup, 

55 TaskInstanceState as TaskInstanceState, 

56 TriggerRule as TriggerRule, 

57 Variable as Variable, 

58 WeightRule as WeightRule, 

59 XComArg as XComArg, 

60 chain as chain, 

61 chain_linear as chain_linear, 

62 cross_downstream as cross_downstream, 

63 dag as dag, 

64 get_current_context as get_current_context, 

65 get_parsing_context as get_parsing_context, 

66 setup as setup, 

67 task as task, 

68 task_group as task_group, 

69 teardown as teardown, 

70 ) 

71 from airflow.sdk.bases.decorator import ( 

72 DecoratedMappedOperator as DecoratedMappedOperator, 

73 DecoratedOperator as DecoratedOperator, 

74 TaskDecorator as TaskDecorator, 

75 get_unique_task_id as get_unique_task_id, 

76 task_decorator_factory as task_decorator_factory, 

77 ) 

78 from airflow.sdk.bases.sensor import poke_mode_only as poke_mode_only 

79 from airflow.sdk.definitions.context import context_merge as context_merge 

80 from airflow.sdk.definitions.mappedoperator import MappedOperator as MappedOperator 

81 from airflow.sdk.definitions.template import literal as literal 

82 from airflow.sdk.exceptions import ( 

83 AirflowException as AirflowException, 

84 AirflowFailException as AirflowFailException, 

85 AirflowNotFoundException as AirflowNotFoundException, 

86 AirflowSensorTimeout as AirflowSensorTimeout, 

87 AirflowSkipException as AirflowSkipException, 

88 AirflowTaskTimeout as AirflowTaskTimeout, 

89 ParamValidationError as ParamValidationError, 

90 TaskDeferred as TaskDeferred, 

91 XComNotFound as XComNotFound, 

92 ) 

93 from airflow.sdk.observability.stats import Stats # noqa: F401 

94 

95 # Airflow 3-only exceptions (conditionally imported) 

96 if AIRFLOW_V_3_0_PLUS: 

97 from airflow.sdk.exceptions import ( 

98 DagRunTriggerException as DagRunTriggerException, 

99 DownstreamTasksSkipped as DownstreamTasksSkipped, 

100 ) 

101 from airflow.sdk.execution_time.context import ( 

102 AIRFLOW_VAR_NAME_FORMAT_MAPPING as AIRFLOW_VAR_NAME_FORMAT_MAPPING, 

103 context_to_airflow_vars as context_to_airflow_vars, 

104 ) 

105 from airflow.sdk.execution_time.timeout import timeout as timeout 

106 from airflow.sdk.execution_time.xcom import XCom as XCom 

107 

108 

109from airflow.providers.common.compat._compat_utils import create_module_getattr 

110 

111# Rename map for classes that changed names between Airflow 2.x and 3.x 

112# Format: new_name -> (new_path, old_path, old_name) 

113_RENAME_MAP: dict[str, tuple[str, str, str]] = { 

114 # Assets: Dataset -> Asset rename in Airflow 3.0 

115 "Asset": ("airflow.sdk", "airflow.datasets", "Dataset"), 

116 "AssetAlias": ("airflow.sdk", "airflow.datasets", "DatasetAlias"), 

117 "AssetAll": ("airflow.sdk", "airflow.datasets", "DatasetAll"), 

118 "AssetAny": ("airflow.sdk", "airflow.datasets", "DatasetAny"), 

119} 

120 

121# Import map for classes/functions/constants 

122# Format: class_name -> module_path(s) 

123# - str: single module path (no fallback) 

124# - tuple[str, ...]: multiple module paths (try in order, newest first) 

125_IMPORT_MAP: dict[str, str | tuple[str, ...]] = { 

126 # ============================================================================ 

127 # Hooks 

128 # ============================================================================ 

129 "BaseHook": ("airflow.sdk", "airflow.hooks.base"), 

130 # ============================================================================ 

131 # Sensors 

132 # ============================================================================ 

133 "BaseSensorOperator": ("airflow.sdk", "airflow.sensors.base"), 

134 "PokeReturnValue": ("airflow.sdk", "airflow.sensors.base"), 

135 "poke_mode_only": ("airflow.sdk.bases.sensor", "airflow.sensors.base"), 

136 # ============================================================================ 

137 # Operators 

138 # ============================================================================ 

139 "BaseOperator": ("airflow.sdk", "airflow.models.baseoperator"), 

140 # ============================================================================ 

141 # Decorators 

142 # ============================================================================ 

143 "task": ("airflow.sdk", "airflow.decorators"), 

144 "dag": ("airflow.sdk", "airflow.decorators"), 

145 "task_group": ("airflow.sdk", "airflow.decorators"), 

146 "setup": ("airflow.sdk", "airflow.decorators"), 

147 "teardown": ("airflow.sdk", "airflow.decorators"), 

148 "TaskDecorator": ("airflow.sdk.bases.decorator", "airflow.decorators"), 

149 "task_decorator_factory": ("airflow.sdk.bases.decorator", "airflow.decorators.base"), 

150 "get_unique_task_id": ("airflow.sdk.bases.decorator", "airflow.decorators.base"), 

151 # ============================================================================ 

152 # Models 

153 # ============================================================================ 

154 "Connection": ("airflow.sdk", "airflow.models.connection"), 

155 "Variable": ("airflow.sdk", "airflow.models.variable"), 

156 "XCom": ("airflow.sdk.execution_time.xcom", "airflow.models.xcom"), 

157 "DAG": ("airflow.sdk", "airflow.models.dag"), 

158 "Param": ("airflow.sdk", "airflow.models.param"), 

159 "XComArg": ("airflow.sdk", "airflow.models.xcom_arg"), 

160 "DecoratedOperator": ("airflow.sdk.bases.decorator", "airflow.decorators.base"), 

161 "DecoratedMappedOperator": ("airflow.sdk.bases.decorator", "airflow.decorators.base"), 

162 "MappedOperator": ("airflow.sdk.definitions.mappedoperator", "airflow.models.mappedoperator"), 

163 # ============================================================================ 

164 # Assets (Dataset → Asset rename in Airflow 3.0) 

165 # ============================================================================ 

166 # Note: Asset, AssetAlias, AssetAll, AssetAny are handled by _RENAME_MAP 

167 # Metadata moved from airflow.datasets.metadata (2.x) to airflow.sdk (3.x) 

168 "Metadata": ("airflow.sdk", "airflow.datasets.metadata"), 

169 # ============================================================================ 

170 # Notifiers 

171 # ============================================================================ 

172 "BaseNotifier": ("airflow.sdk", "airflow.notifications.basenotifier"), 

173 # ============================================================================ 

174 # Operator Links & Task Groups 

175 # ============================================================================ 

176 "BaseOperatorLink": ("airflow.sdk", "airflow.models.baseoperatorlink"), 

177 "TaskGroup": ("airflow.sdk", "airflow.utils.task_group"), 

178 # ============================================================================ 

179 # Operator Utilities (chain, cross_downstream, etc.) 

180 # ============================================================================ 

181 "chain": ("airflow.sdk", "airflow.models.baseoperator"), 

182 "chain_linear": ("airflow.sdk", "airflow.models.baseoperator"), 

183 "cross_downstream": ("airflow.sdk", "airflow.models.baseoperator"), 

184 # ============================================================================ 

185 # Edge Modifiers & Labels 

186 # ============================================================================ 

187 "EdgeModifier": ("airflow.sdk", "airflow.utils.edgemodifier"), 

188 "Label": ("airflow.sdk", "airflow.utils.edgemodifier"), 

189 # ============================================================================ 

190 # State Enums 

191 # ============================================================================ 

192 "DagRunState": ("airflow.sdk", "airflow.utils.state"), 

193 "TaskInstanceState": ("airflow.sdk", "airflow.utils.state"), 

194 "TriggerRule": ("airflow.sdk", "airflow.utils.trigger_rule"), 

195 "WeightRule": ("airflow.sdk", "airflow.utils.weight_rule"), 

196 # ============================================================================ 

197 # IO & Storage 

198 # ============================================================================ 

199 "ObjectStoragePath": ("airflow.sdk", "airflow.io.path"), 

200 # ============================================================================ 

201 # Template Utilities 

202 # ============================================================================ 

203 "literal": ("airflow.sdk.definitions.template", "airflow.utils.template"), 

204 # ============================================================================ 

205 # Context & Utilities 

206 # ============================================================================ 

207 "Context": ("airflow.sdk", "airflow.utils.context"), 

208 "context_merge": ("airflow.sdk.definitions.context", "airflow.utils.context"), 

209 "context_to_airflow_vars": ("airflow.sdk.execution_time.context", "airflow.utils.operator_helpers"), 

210 "AIRFLOW_VAR_NAME_FORMAT_MAPPING": ( 

211 "airflow.sdk.execution_time.context", 

212 "airflow.utils.operator_helpers", 

213 ), 

214 "get_current_context": ("airflow.sdk", "airflow.operators.python"), 

215 "get_parsing_context": ("airflow.sdk", "airflow.utils.dag_parsing_context"), 

216 # ============================================================================ 

217 # Timeout Utilities 

218 # ============================================================================ 

219 "timeout": ("airflow.sdk.execution_time.timeout", "airflow.utils.timeout"), 

220 # ============================================================================ 

221 # XCom & Task Communication 

222 # ============================================================================ 

223 "XCOM_RETURN_KEY": "airflow.models.xcom", 

224 # ============================================================================ 

225 # Exceptions (deprecated in airflow.exceptions, prefer SDK) 

226 # ============================================================================ 

227 # Note: AirflowException and AirflowNotFoundException are not deprecated, but exposing them 

228 # here keeps provider imports consistent across Airflow 2 and 3. 

229 "AirflowException": ("airflow.sdk.exceptions", "airflow.exceptions"), 

230 "AirflowFailException": ("airflow.sdk.exceptions", "airflow.exceptions"), 

231 "AirflowNotFoundException": ("airflow.sdk.exceptions", "airflow.exceptions"), 

232 "AirflowSkipException": ("airflow.sdk.exceptions", "airflow.exceptions"), 

233 "AirflowTaskTimeout": ("airflow.sdk.exceptions", "airflow.exceptions"), 

234 "AirflowSensorTimeout": ("airflow.sdk.exceptions", "airflow.exceptions"), 

235 "ParamValidationError": ("airflow.sdk.exceptions", "airflow.exceptions"), 

236 "TaskDeferred": ("airflow.sdk.exceptions", "airflow.exceptions"), 

237 "XComNotFound": ("airflow.sdk.exceptions", "airflow.exceptions"), 

238 # ============================================================================ 

239 # Observability 

240 # ============================================================================ 

241 "Stats": ("airflow.sdk.observability.stats", "airflow.stats"), 

242} 

243 

244# Airflow 3-only exceptions (not available in Airflow 2) 

245_AIRFLOW_3_ONLY_EXCEPTIONS: dict[str, tuple[str, ...]] = { 

246 "DownstreamTasksSkipped": ("airflow.sdk.exceptions", "airflow.exceptions"), 

247 "DagRunTriggerException": ("airflow.sdk.exceptions", "airflow.exceptions"), 

248} 

249 

250# Add Airflow 3-only exceptions to _IMPORT_MAP if running Airflow 3+ 

251if AIRFLOW_V_3_0_PLUS: 

252 _IMPORT_MAP.update(_AIRFLOW_3_ONLY_EXCEPTIONS) 

253 

254# Module map: module_name -> module_path(s) 

255# For entire modules that have been moved (e.g., timezone) 

256# Usage: from airflow.providers.common.compat.lazy_compat import timezone 

257_MODULE_MAP: dict[str, str | tuple[str, ...]] = { 

258 "timezone": ("airflow.sdk.timezone", "airflow.utils.timezone"), 

259 "io": ("airflow.sdk.io", "airflow.io"), 

260} 

261 

262# Use the shared utility to create __getattr__ 

263__getattr__ = create_module_getattr( 

264 import_map=_IMPORT_MAP, 

265 module_map=_MODULE_MAP, 

266 rename_map=_RENAME_MAP, 

267) 

268 

269__all__ = list(_RENAME_MAP.keys()) + list(_IMPORT_MAP.keys()) + list(_MODULE_MAP.keys())