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 2019/12/12 18:18:01 UTC

[GitHub] [airflow] dhuang commented on a change in pull request #6793: [AIRFLOW-1076] Add get method for template variable accessor

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

 ##########
 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,
 
 Review comment:
   yep had thought I was actually using `_getattr_` when I was doing `self.var` in `get()`, removed.

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