Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/airflow/__init__.py: 63%

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

43 statements  

1# 

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

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

4# distributed with this work for additional information 

5# regarding copyright ownership. The ASF licenses this file 

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

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

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

9# 

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

11# 

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

13# software distributed under the License is distributed on an 

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

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

16# specific language governing permissions and limitations 

17# under the License. 

18 

19# We do not use "from __future__ import annotations" here because it is not supported 

20# by Pycharm when we want to make sure all imports in airflow work from namespace packages 

21# Adding it automatically is excluded in pyproject.toml via I002 ruff rule exclusion 

22 

23# Make `airflow` a namespace package, supporting installing 

24# airflow.providers.* in different locations (i.e. one in site, and one in user 

25# lib.) This is required by some IDEs to resolve the import paths. 

26__path__ = __import__("pkgutil").extend_path(__path__, __name__) 

27 

28__version__ = "3.2.0" 

29 

30 

31import os 

32import sys 

33import warnings 

34from typing import TYPE_CHECKING 

35 

36from airflow.utils.deprecation_tools import DeprecatedImportWarning 

37 

38if os.environ.get("_AIRFLOW_PATCH_GEVENT"): 

39 # If you are using gevents and start airflow webserver, you might want to run gevent monkeypatching 

40 # as one of the first thing when Airflow is started. This allows gevent to patch networking and other 

41 # system libraries to make them gevent-compatible before anything else patches them (for example boto) 

42 from gevent.monkey import patch_all 

43 

44 patch_all() 

45 

46if sys.platform == "win32": 

47 warnings.warn( 

48 "Airflow currently can be run on POSIX-compliant Operating Systems. For development, " 

49 "it is regularly tested on fairly modern Linux Distros and recent versions of macOS. " 

50 "On Windows you can run it via WSL2 (Windows Subsystem for Linux 2) or via Linux Containers. " 

51 "The work to add Windows support is tracked via https://github.com/apache/airflow/issues/10388, " 

52 "but it is not a high priority.", 

53 category=RuntimeWarning, 

54 stacklevel=1, 

55 ) 

56 

57# The configuration module initializes and validates the conf object as a side effect the first 

58# time it is imported. If it is not imported before importing the settings module, the conf 

59# object will then be initted/validated as a side effect of it being imported in settings, 

60# however this can cause issues since those modules are very tightly coupled and can 

61# very easily cause import cycles in the conf init/validate code (since downstream code from 

62# those functions likely import settings). 

63# configuration is therefore initted early here, simply by importing it. 

64from airflow import configuration, settings 

65 

66__all__ = [ 

67 "__version__", 

68 "DAG", 

69 "Asset", 

70 "XComArg", 

71 # TODO: Remove this module in Airflow 3.2 

72 "Dataset", 

73] 

74 

75# Perform side-effects unless someone has explicitly opted out before import 

76# WARNING: DO NOT USE THIS UNLESS YOU REALLY KNOW WHAT YOU'RE DOING. 

77# This environment variable prevents proper initialization, and things like 

78# configs, logging, the ORM, etc. will be broken. It is only useful if you only 

79# access certain trivial constants and free functions (e.g. `__version__`). 

80if not os.environ.get("_AIRFLOW__AS_LIBRARY", None): 

81 settings.initialize() 

82 

83# Things to lazy import in form {local_name: ('target_module', 'target_name', 'deprecated')} 

84__lazy_imports: dict[str, tuple[str, str, bool]] = { 

85 "DAG": (".sdk", "DAG", False), 

86 "Asset": (".sdk", "Asset", False), 

87 "XComArg": (".models.xcom_arg", "XComArg", False), 

88 "version": (".version", "", False), 

89 # Deprecated lazy imports 

90 "AirflowException": (".exceptions", "AirflowException", True), 

91 "Dataset": (".sdk", "Asset", True), 

92 "Stats": (".observability.stats", "Stats", True), 

93 "Trace": (".observability.trace", "Trace", True), 

94 "metrics": (".observability.metrics", "", True), 

95 "traces": (".observability.traces", "", True), 

96} 

97if TYPE_CHECKING: 

98 # These objects are imported by PEP-562, however, static analyzers and IDE's 

99 # have no idea about typing of these objects. 

100 # Add it under TYPE_CHECKING block should help with it. 

101 from airflow.models.xcom_arg import XComArg 

102 from airflow.sdk import DAG, Asset, Asset as Dataset 

103 

104 

105def __getattr__(name: str): 

106 # PEP-562: Lazy loaded attributes on python modules 

107 module_path, attr_name, deprecated = __lazy_imports.get(name, ("", "", False)) 

108 if not module_path: 

109 raise AttributeError(f"module {__name__!r} has no attribute {name!r}") 

110 if deprecated: 

111 warnings.warn( 

112 f"Import {name!r} directly from the airflow module is deprecated and " 

113 f"will be removed in the future. Please import it from 'airflow{module_path}.{attr_name}'.", 

114 DeprecatedImportWarning, 

115 stacklevel=2, 

116 ) 

117 

118 import importlib 

119 

120 mod = importlib.import_module(module_path, __name__) 

121 if attr_name: 

122 val = getattr(mod, attr_name) 

123 else: 

124 val = mod 

125 

126 # Store for next time 

127 globals()[name] = val 

128 return val 

129 

130 

131if not settings.LAZY_LOAD_PROVIDERS: 

132 from airflow.providers_manager import ProvidersManager 

133 

134 manager = ProvidersManager() 

135 manager.initialize_providers_list() 

136 manager.initialize_providers_hooks() 

137 manager.initialize_providers_extra_links() 

138if not settings.LAZY_LOAD_PLUGINS: 

139 from airflow import plugins_manager 

140 

141 plugins_manager.ensure_plugins_loaded()