ashb commented on a change in pull request #4819: [AIRFLOW-3997] Add accessor 
for optional variables
URL: https://github.com/apache/airflow/pull/4819#discussion_r262691620
 
 

 ##########
 File path: airflow/models/__init__.py
 ##########
 @@ -4263,6 +4263,14 @@ def setdefault(cls, key, default, 
deserialize_json=False):
         else:
             return obj
 
+    @classmethod
+    @provide_session
+    def try_get(cls, key, deserialize_json=False, session=None):
+        try:
+            return cls.get(key, deserialize_json=deserialize_json, 
session=session)
+        except KeyError:
+            return None
 
 Review comment:
   I'd like to do this without an extra method. What I propose is:
   
   ```
       NO_DEFAULT_SENTINEL = object()
   
        def get(cls, key, default_var=NO_DEFAULT_SENTINEL, 
deserialize_json=False, session=None):
           obj = session.query(cls).filter(cls.key == key).first()
           if obj is None:
               if default_var is not NO_DEFAULT_SENTINEL:
                   return default_var
               ...
   ```

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to