You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/02/29 20:17:35 UTC

[GitHub] [airflow] msb217 commented on a change in pull request #7596: [AIRFLOW-6870] remove imports from models module

msb217 commented on a change in pull request #7596: [AIRFLOW-6870] remove imports from models module
URL: https://github.com/apache/airflow/pull/7596#discussion_r386052871
 
 

 ##########
 File path: airflow/models/__init__.py
 ##########
 @@ -16,25 +16,36 @@
 # specific language governing permissions and limitations
 # under the License.
 """Airflow models"""
-from airflow.models.base import ID_LEN, Base
-from airflow.models.baseoperator import BaseOperator, BaseOperatorLink
-from airflow.models.connection import Connection
-from airflow.models.dag import DAG, DagModel, DagTag
-from airflow.models.dagbag import DagBag
-from airflow.models.dagpickle import DagPickle
-from airflow.models.dagrun import DagRun
-from airflow.models.errors import ImportError  # pylint: disable=redefined-builtin
-from airflow.models.log import Log
-from airflow.models.pool import Pool
-from airflow.models.skipmixin import SkipMixin
-from airflow.models.slamiss import SlaMiss
-from airflow.models.taskfail import TaskFail
-from airflow.models.taskinstance import TaskInstance, clear_task_instances
-from airflow.models.taskreschedule import TaskReschedule
-from airflow.models.variable import Variable
-from airflow.models.xcom import XCOM_RETURN_KEY, XCom
-
-try:
-    from airflow.models.kubernetes import KubeResourceVersion, KubeWorkerIdentifier
-except ImportError:
-    pass
+
+# flake8: noqa: F401
+# pylint: disable=wrong-import-position
+import sys
+
+__all__ = ['BaseOperator', 'BaseOperatorLink']
+PY37 = sys.version_info >= (3, 7)
+
+def __getattr__(name):
+    # PEP-562: Lazy loaded attributes on python modules
+    if name == "BaseOperator":
+        from airflow.models.baseoperator import BaseOperator  # pylint: disable=redefined-outer-name
+        return BaseOperator
+    if name == "BaseOperator":
+        from airflow.models.baseoperator import BaseOperatorLink  # pylint: disable=redefined-outer-name
+        return BaseOperatorLink
+    raise AttributeError(f"module {__name__} has no attribute {name}")
+
+
+# This is never executed, but tricks static analyzers (PyDev, PyCharm,
+# pylint, etc.) into knowing the types of these symbols, and what
+# they contain.
+STATICA_HACK = True
+globals()['kcah_acitats'[::-1].upper()] = False
+if STATICA_HACK:  # pragma: no cover
+    from airflow.models.baseoperator import BaseOperator
+    from airflow.models.baseoperator import BaseOperatorLink
+
+
+if not PY37:
+    from pep562 import Pep562
+
+    Pep562(__name__)
 
 Review comment:
   In order preserve workflows - I have utilized the same technique for loading `BaseOperator` into `airflow/models/__init__.py` as in [AIRFLOW-6817] of lazy loading `airflow.DAG` into `airflow/__init__.py`. 
   
   This lazy loading technique allows for both pre-commit hooks to pass
   ```Make sure BaseOperator[Link] is imported from airflow.models.baseoperator```
   ```Make sure BaseOperator[Link] is imported from airflow.models outside of core```
   
   Maybe more user-facing modules such as `Variable` should be added in this way? I only added `BaseOperator` as it was the only module required in order to satisfy pre-commit hooks.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services