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 2023/01/05 12:08:45 UTC

[GitHub] [airflow] uranusjr opened a new pull request, #28742: Fix serialize() usages in custom JSON encoders

uranusjr opened a new pull request, #28742:
URL: https://github.com/apache/airflow/pull/28742

   Fix #28741.
   
   Our custom JSON encoders call `serialize()` in their `default()` hooks. However, `serialize()` does not guarantee to return a JSON-serializable type, and when it does not (e.g. a set object), the JSON encoder would infinitely recurse into `serialize()` trying to coerce the object into JSON-serializable.
   
   This patch adds an additional `ensure_json_compatible` parameter to `serialize()`, so JSON encoders can use it to require `serialize()` to always return JSON-compatible values.
   
   Some minor refactoring is also applied to `serialize()` to ensure the flag is correctly passed down through recursive visits, and also hide the `depth` tracking parameter (which is internal to the function) from outside access.
   
   The `_reverse_cache` global variable is also deleted since it is not used anywhere, even before this patch.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
uranusjr commented on PR #28742:
URL: https://github.com/apache/airflow/pull/28742#issuecomment-1384707382

   Rethinking this, I _think_ this can simply be achieved by a better implemented `default`. I’ll split out that main change and other minor improvements here into two PRs so things are easier to manage.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] kaxil commented on pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
kaxil commented on PR #28742:
URL: https://github.com/apache/airflow/pull/28742#issuecomment-1372453179

   Running it with full test suite


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] bolkedebruin commented on a diff in pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
bolkedebruin commented on code in PR #28742:
URL: https://github.com/apache/airflow/pull/28742#discussion_r1064944047


##########
airflow/utils/json.py:
##########
@@ -56,12 +56,12 @@ def default(self, o: Any) -> Any:
             return o.strftime("%Y-%m-%d")
 
         if isinstance(o, Decimal):
-            data = serialize(o)
+            data = serialize(o, ensure_json_compatible=True)

Review Comment:
   As mentioned, this should not be done this way, but rather be solved here.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] bolkedebruin commented on a diff in pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
bolkedebruin commented on code in PR #28742:
URL: https://github.com/apache/airflow/pull/28742#discussion_r1064954150


##########
airflow/serialization/serde.py:
##########
@@ -72,89 +70,104 @@ def decode(d: dict[str, str | int | T]) -> tuple:
     return d[CLASSNAME], d[VERSION], d.get(DATA, None)
 
 
-def serialize(o: object, depth: int = 0) -> U | None:
+@dataclasses.dataclass()

Review Comment:
   This adds calling overhead, which we don't want in this code.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr closed pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
uranusjr closed pull request #28742: Fix serialize() usages in custom JSON encoders
URL: https://github.com/apache/airflow/pull/28742


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on a diff in pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
uranusjr commented on code in PR #28742:
URL: https://github.com/apache/airflow/pull/28742#discussion_r1062405932


##########
airflow/serialization/serde.py:
##########
@@ -57,11 +57,9 @@
 _extra_allowed: set[str] = set()
 
 _primitives = (int, bool, float, str)
-_iterables = (list, set, tuple)
+_collections = (list, set, tuple)  # dict is treated specially.

Review Comment:
   Renamed to better reflect these classes. _Iterable_ has special meaning in Python (collection also does, but its meaning is more in-line with what we mean here).



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] bolkedebruin commented on pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
bolkedebruin commented on PR #28742:
URL: https://github.com/apache/airflow/pull/28742#issuecomment-1375816271

   I'll have a look at this. However, serialize should return a dict that is able to be JSON-ized. But importantly do not put any serialization code in a class. Classes are slow unfortunately on the stack. 


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] bolkedebruin commented on a diff in pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
bolkedebruin commented on code in PR #28742:
URL: https://github.com/apache/airflow/pull/28742#discussion_r1064942679


##########
airflow/serialization/serde.py:
##########
@@ -176,11 +189,11 @@ def deserialize(o: T | None, full=True, type_hint: Any = None) -> object:
     if isinstance(o, _primitives):
         return o
 
-    if isinstance(o, _iterables):
+    if isinstance(o, _collections):
         return [deserialize(d) for d in o]
 
     if not isinstance(o, dict):
-        raise TypeError()
+        raise TypeError("not deserializable")

Review Comment:
   It is probably better to explain what type we are getting if we are improving the error message,.



##########
airflow/serialization/serde.py:
##########
@@ -249,23 +262,19 @@ def _convert(old: dict) -> dict:
 
 
 def _match(classname: str) -> bool:
-    for p in _patterns:
-        if p.match(classname):
-            return True
-
-    return False
+    return any(p.match(classname) is not None for p in _patterns)

Review Comment:
   nice!



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] bolkedebruin commented on a diff in pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
bolkedebruin commented on code in PR #28742:
URL: https://github.com/apache/airflow/pull/28742#discussion_r1064943244


##########
airflow/serialization/serde.py:
##########
@@ -249,23 +262,19 @@ def _convert(old: dict) -> dict:
 
 
 def _match(classname: str) -> bool:
-    for p in _patterns:
-        if p.match(classname):
-            return True
-
-    return False
+    return any(p.match(classname) is not None for p in _patterns)
 
 
 def _stringify(classname: str, version: int, value: T | None) -> str:
     s = f"{classname}@version={version}("
     if isinstance(value, _primitives):
         s += f"{value})"
-    elif isinstance(value, _iterables):
-        s += ",".join(str(serialize(value, False)))
     elif isinstance(value, dict):
         for k, v in value.items():
-            s += f"{k}={str(serialize(v, False))},"
+            s += f"{k}={str(serialize(v))},"

Review Comment:
   good catch, i think? :-)



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] bolkedebruin commented on a diff in pull request #28742: Fix serialize() usages in custom JSON encoders

Posted by GitBox <gi...@apache.org>.
bolkedebruin commented on code in PR #28742:
URL: https://github.com/apache/airflow/pull/28742#discussion_r1064945310


##########
airflow/serialization/serde.py:
##########
@@ -72,89 +70,104 @@ def decode(d: dict[str, str | int | T]) -> tuple:
     return d[CLASSNAME], d[VERSION], d.get(DATA, None)
 
 
-def serialize(o: object, depth: int = 0) -> U | None:
+@dataclasses.dataclass()
+class _Serializer:

Review Comment:
   Do not put this in a class. Classes are overhead on the stack and significantly slow down serialization/deserialization. The code was implemented on purpose o be as high up in the chain as possible.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org