msumit commented on a change in pull request #17100:
URL: https://github.com/apache/airflow/pull/17100#discussion_r707016819



##########
File path: airflow/models/param.py
##########
@@ -0,0 +1,208 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from typing import Any, Dict, Optional
+
+import jsonschema
+from jsonschema import FormatChecker
+from jsonschema.exceptions import ValidationError
+
+from airflow.exceptions import AirflowException
+
+
+class Param:
+    """
+    Class to hold the default value of a Param and rule set to do the 
validations. Without the rule set
+    it always validates and returns the default value.
+
+    :param default: The value of this Param object holds
+    :type default: Any
+    :param description: Optional help text for the Param
+    :type description: str
+    :param schema: The validation schema of the Param, if not given then all 
kwargs except
+        default & description will form the schema
+    :type schema: dict
+    """
+
+    def __init__(self, default: Any = None, description: str = None, **kwargs):
+        self.default = default
+        self.description = description
+        self.schema = kwargs.pop('schema') if 'schema' in kwargs else kwargs
+
+        # If default is not None, then validate it once, may raise ValueError
+        if default:
+            try:
+                jsonschema.validate(self.default, self.schema, 
format_checker=FormatChecker())
+            except ValidationError as err:
+                raise ValueError(err)
+
+    def resolve(self, value: Optional[Any] = None, suppress_exception: bool = 
False) -> Any:
+        """
+        Runs the validations and returns the Param's final value.
+        May raise ValueError on failed validations.
+
+        :param value: The value to be updated for the Param
+        :type: Optional[Any]
+        :param suppress_exception: To raise an exception or not when the 
validations fails.
+            If true and validations fails, the return value would be None.
+        :type suppress_exception: bool
+        """
+        try:
+            final_val = value or self.default
+            jsonschema.validate(final_val, self.schema, 
format_checker=FormatChecker())
+            self.default = final_val
+        except ValidationError as err:
+            if suppress_exception:
+                return None
+            raise ValueError(err) from None

Review comment:
       To suppress the exception context chain, see 
https://www.python.org/dev/peps/pep-0409




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to