You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ds...@apache.org on 2021/12/16 17:39:55 UTC

[airflow] branch main updated: Add deprecation warnining for non-json-serializable params (#20174)

This is an automated email from the ASF dual-hosted git repository.

dstandish pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 9e56f8d  Add deprecation warnining for non-json-serializable params (#20174)
9e56f8d is described below

commit 9e56f8dbf3a0c7a80f72c2848d663714d48d58e7
Author: Daniel Standish <15...@users.noreply.github.com>
AuthorDate: Thu Dec 16 09:39:22 2021 -0800

    Add deprecation warnining for non-json-serializable params (#20174)
    
    Following vote by lazy consensus (https://lists.apache.org/thread/whqbbo3gh84s7ggn7968mqlk4d41x7zl), we deprecate non-json-serializable params with removal planned for Airflow 3.0.
---
 airflow/models/param.py    | 24 ++++++++++++++++++++++--
 tests/models/test_param.py |  4 ++++
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/airflow/models/param.py b/airflow/models/param.py
index 6d559b5..147048a 100644
--- a/airflow/models/param.py
+++ b/airflow/models/param.py
@@ -15,6 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 import copy
+import json
+import warnings
 from typing import TYPE_CHECKING, Any, Dict, ItemsView, MutableMapping, Optional, ValuesView
 
 import jsonschema
@@ -49,10 +51,28 @@ class Param:
         self.description = description
         self.schema = kwargs.pop('schema') if 'schema' in kwargs else kwargs
 
+        if self.has_value:
+            self._validate(self.value, self.schema)
+
+    @staticmethod
+    def _validate(value, schema):
+        """
+        1. Check that value is json-serializable; if not, warn.  In future release we will require
+        the value to be json-serializable.
+        2. Validate ``value`` against ``schema``
+        """
+        try:
+            json.dumps(value)
+        except Exception:
+            warnings.warn(
+                "The use of non-json-serializable params is deprecated and will be removed in "
+                "a future release",
+                DeprecationWarning,
+            )
         # If we have a value, validate it once. May raise ValueError.
-        if not isinstance(default, ArgNotSet):
+        if not isinstance(value, ArgNotSet):
             try:
-                jsonschema.validate(self.value, self.schema, format_checker=FormatChecker())
+                jsonschema.validate(value, schema, format_checker=FormatChecker())
             except ValidationError as err:
                 raise ValueError(err)
 
diff --git a/tests/models/test_param.py b/tests/models/test_param.py
index eb1c78c..6725a3a 100644
--- a/tests/models/test_param.py
+++ b/tests/models/test_param.py
@@ -267,3 +267,7 @@ class TestDagParamRuntime:
 
         ti = dr.get_task_instances()[0]
         assert ti.xcom_pull() == 'test'
+
+    def test_param_non_json_serializable(self):
+        with pytest.warns(DeprecationWarning, match='The use of non-json-serializable params is deprecated'):
+            Param(default={0, 1, 2})