airflow-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [airflow] ashb commented on a change in pull request #6793: [AIRFLOW-1076] Add get method for template variable accessor
Date Thu, 12 Dec 2019 12:23:27 GMT
ashb commented on a change in pull request #6793: [AIRFLOW-1076] Add get method for template
variable accessor
URL: https://github.com/apache/airflow/pull/6793#discussion_r357111176
 
 

 ##########
 File path: airflow/models/taskinstance.py
 ##########
 @@ -1178,34 +1178,60 @@ def get_template_context(self, session=None) -> Dict[str, Any]:
 
         class VariableAccessor:
             """
-            Wrapper around Variable. This way you can get variables in templates by using
-            {var.value.your_variable_name}.
+            Wrapper around Variable. This way you can get variables in
+            templates by using {{ var.value.variable_name }} or
+            {{ var.value.get('variable_name', 'fallback') }}.
             """
             def __init__(self):
                 self.var = None
 
-            def __getattr__(self, item):
+            def __getattr__(
+                self,
+                item: str,
+                default_var: Any = Variable._Variable__NO_DEFAULT_SENTINEL,
+            ):
                 self.var = Variable.get(item)
                 return self.var
 
             def __repr__(self):
                 return str(self.var)
 
+            @staticmethod
+            def get(
+                item: str,
+                default_var: Any = Variable._Variable__NO_DEFAULT_SENTINEL,
+            ):
+                self.var = Variable.get(item, default_var=default_var)
+                return self.var
+
         class VariableJsonAccessor:
             """
-            Wrapper around deserialized Variables. This way you can get variables
-            in templates by using {var.json.your_variable_name}.
+            Wrapper around Variable. This way you can get variables in
+            templates by using {{ var.json.variable_name }} or
+            {{ var.json.get('variable_name', {'fall': 'back'}) }}.
             """
             def __init__(self):
                 self.var = None
 
-            def __getattr__(self, item):
-                self.var = Variable.get(item, deserialize_json=True)
+            def __getattr__(
+                self,
+                item: str,
+                default_var: Any = Variable._Variable__NO_DEFAULT_SENTINEL,
+            ):
+                self.var = Variable.get(item, default_var=default_var, deserialize_json=True)
                 return self.var
 
             def __repr__(self):
                 return str(self.var)
 
+            @staticmethod
+            def get(
+                item: str,
+                default_var: Any = Variable._Variable__NO_DEFAULT_SENTINEL,
+            ):
+                self.var = Variable.get(item, default_var=default_var, deserialize_json=True)
 
 Review comment:
   This is a static method, there is no `self` in local scope, so this is actually the TaskInstance.
This is not what you want.
   
   ```suggestion
                   return Variable.get(item, default_var=default_var, deserialize_json=True)
   ```

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

Mime
View raw message