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 2022/04/08 14:33:28 UTC

[GitHub] [airflow] jedcunningham commented on a diff in pull request #22862: Add XComArg to lazy-imported list of Airflow module

jedcunningham commented on code in PR #22862:
URL: https://github.com/apache/airflow/pull/22862#discussion_r846172883


##########
airflow/__init__.py:
##########
@@ -54,18 +54,30 @@
 PY39 = sys.version_info >= (3, 9)
 PY310 = sys.version_info >= (3, 10)
 
+# Things to lazy import in form 'name': 'path.to.module'
+__lazy_imports = {
+    'DAG': 'airflow.models.dag',
+    'XComArg': 'airflow.models.xcom_arg',
+    'AirflowException': 'airflow.exceptions',
+}
+
 
 def __getattr__(name):
     # PEP-562: Lazy loaded attributes on python modules
-    if name == "DAG":
-        from airflow.models.dag import DAG
+    path = __lazy_imports.get(name)
+    if path:
+        import operator

Review Comment:
   ```suggestion
       if not path:
           raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
   
       import operator
   ```
   
   nit



##########
airflow/__init__.py:
##########
@@ -54,18 +54,30 @@
 PY39 = sys.version_info >= (3, 9)
 PY310 = sys.version_info >= (3, 10)
 
+# Things to lazy import in form 'name': 'path.to.module'
+__lazy_imports = {
+    'DAG': 'airflow.models.dag',
+    'XComArg': 'airflow.models.xcom_arg',
+    'AirflowException': 'airflow.exceptions',
+}
+
 
 def __getattr__(name):
     # PEP-562: Lazy loaded attributes on python modules
-    if name == "DAG":
-        from airflow.models.dag import DAG
+    path = __lazy_imports.get(name)
+    if path:
+        import operator
 
-        return DAG
-    if name == "AirflowException":
-        from airflow.exceptions import AirflowException
+        # Strip of the "airflow." prefix because of how `__import__` works (it always returns the top level
+        # module)
+        without_prefix = path.split('.', 1)[-1]

Review Comment:
   ```suggestion
       # Strip of the "airflow." prefix because of how `__import__` works (it always returns the top level
       # module)
       without_prefix = path.split('.', 1)[-1]
   ```
   nit cont.



##########
airflow/__init__.py:
##########
@@ -54,18 +54,30 @@
 PY39 = sys.version_info >= (3, 9)
 PY310 = sys.version_info >= (3, 10)
 
+# Things to lazy import in form 'name': 'path.to.module'
+__lazy_imports = {
+    'DAG': 'airflow.models.dag',
+    'XComArg': 'airflow.models.xcom_arg',
+    'AirflowException': 'airflow.exceptions',
+}
+
 
 def __getattr__(name):
     # PEP-562: Lazy loaded attributes on python modules
-    if name == "DAG":
-        from airflow.models.dag import DAG
+    path = __lazy_imports.get(name)
+    if path:
+        import operator
 
-        return DAG
-    if name == "AirflowException":
-        from airflow.exceptions import AirflowException
+        # Strip of the "airflow." prefix because of how `__import__` works (it always returns the top level
+        # module)
+        without_prefix = path.split('.', 1)[-1]
 
-        return AirflowException
-    raise AttributeError(f"module {__name__} has no attribute {name}")
+        getter = operator.attrgetter(f'{without_prefix}.{name}')
+        val = getter(__import__(path))
+        # Store for next time
+        globals()[name] = val
+        return val
+    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

Review Comment:
   ```suggestion
       getter = operator.attrgetter(f'{without_prefix}.{name}')
       val = getter(__import__(path))
       # Store for next time
       globals()[name] = val
       return val
   ```
   nit cont.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org