amoghrajesh commented on code in PR #77523:
URL: https://github.com/apache/airflow/pull/77523#discussion_r4676838586


##########
task-sdk/src/airflow/sdk/serde/serializers/kubernetes.py:
##########
@@ -55,10 +57,45 @@ def safe_get_name(pod):
                 return None
 
         try:
-            return PodGenerator.serialize_pod(o), qualname(o), __version__, 
True
+            return ApiClient().sanitize_for_serialization(o), qualname(o), 
__version__, True
         except Exception:
             log.warning("Serialization failed for pod %s", safe_get_name(o))
             log.debug("traceback for serialization error", exc_info=True)
             return "", "", 0, False
 
     return "", "", 0, False
+
+
+def deserialize(cls: type, version: int, data: dict) -> Any:
+    import pydantic
+
+    if version > __version__:
+        raise TypeError(f"serialized version {version} is newer than class 
version {__version__}")
+
+    if not hasattr(cls, "model_validate"):

Review Comment:
   [review 
comments](https://github.com/apache/airflow/pull/77523/commits/76b368c0ca664dd05d08a592cfb2811944712f41)



##########
task-sdk/src/airflow/sdk/serde/serializers/kubernetes.py:
##########
@@ -55,10 +57,45 @@ def safe_get_name(pod):
                 return None
 
         try:
-            return PodGenerator.serialize_pod(o), qualname(o), __version__, 
True
+            return ApiClient().sanitize_for_serialization(o), qualname(o), 
__version__, True
         except Exception:
             log.warning("Serialization failed for pod %s", safe_get_name(o))
             log.debug("traceback for serialization error", exc_info=True)
             return "", "", 0, False
 
     return "", "", 0, False
+
+
+def deserialize(cls: type, version: int, data: dict) -> Any:
+    import pydantic

Review Comment:
   [review 
comments](https://github.com/apache/airflow/pull/77523/commits/76b368c0ca664dd05d08a592cfb2811944712f41)



##########
task-sdk/src/airflow/sdk/serde/serializers/kubernetes.py:
##########
@@ -55,10 +57,45 @@ def safe_get_name(pod):
                 return None
 
         try:
-            return PodGenerator.serialize_pod(o), qualname(o), __version__, 
True
+            return ApiClient().sanitize_for_serialization(o), qualname(o), 
__version__, True
         except Exception:
             log.warning("Serialization failed for pod %s", safe_get_name(o))
             log.debug("traceback for serialization error", exc_info=True)
             return "", "", 0, False
 
     return "", "", 0, False
+
+
+def deserialize(cls: type, version: int, data: dict) -> Any:
+    import pydantic
+
+    if version > __version__:
+        raise TypeError(f"serialized version {version} is newer than class 
version {__version__}")
+
+    if not hasattr(cls, "model_validate"):
+        raise TypeError(f"do not know how to deserialize {qualname(cls)}")
+
+    try:
+        return cls.model_validate(data)
+    except pydantic.ValidationError as e:
+        try:
+            coerced = _coerce_legacy_values(data, e)
+        except (KeyError, IndexError, TypeError):
+            raise e
+        return cls.model_validate(coerced)
+
+
+def _coerce_legacy_values(data: dict, error: pydantic.ValidationError) -> dict:
+    # kubernetes<37 stored numeric quantities/labels and string booleans that 
the
+    # pydantic models of kubernetes>=37 reject in strict mode.
+    for err in error.errors():
+        *path, key = err["loc"]
+        parent: Any = data
+        for part in path:
+            parent = parent[part]
+        value = parent[key]
+        if err["type"] == "string_type" and isinstance(value, (int, float)):

Review Comment:
   [review 
comments](https://github.com/apache/airflow/pull/77523/commits/76b368c0ca664dd05d08a592cfb2811944712f41)



##########
task-sdk/src/airflow/sdk/serde/serializers/kubernetes.py:
##########
@@ -55,10 +57,45 @@ def safe_get_name(pod):
                 return None
 
         try:
-            return PodGenerator.serialize_pod(o), qualname(o), __version__, 
True
+            return ApiClient().sanitize_for_serialization(o), qualname(o), 
__version__, True
         except Exception:
             log.warning("Serialization failed for pod %s", safe_get_name(o))
             log.debug("traceback for serialization error", exc_info=True)
             return "", "", 0, False
 
     return "", "", 0, False
+
+
+def deserialize(cls: type, version: int, data: dict) -> Any:
+    import pydantic
+
+    if version > __version__:
+        raise TypeError(f"serialized version {version} is newer than class 
version {__version__}")
+
+    if not hasattr(cls, "model_validate"):
+        raise TypeError(f"do not know how to deserialize {qualname(cls)}")
+
+    try:
+        return cls.model_validate(data)
+    except pydantic.ValidationError as e:
+        try:
+            coerced = _coerce_legacy_values(data, e)
+        except (KeyError, IndexError, TypeError):

Review Comment:
   [review 
comments](https://github.com/apache/airflow/pull/77523/commits/76b368c0ca664dd05d08a592cfb2811944712f41)



##########
task-sdk/src/airflow/sdk/serde/serializers/kubernetes.py:
##########
@@ -29,11 +29,13 @@
 ]
 
 if TYPE_CHECKING:
+    import pydantic
+
     from airflow.sdk.serde import U
 
 __version__ = 1
 
-deserializers: list[type[object]] = []
+deserializers = serializers

Review Comment:
   [review 
comments](https://github.com/apache/airflow/pull/77523/commits/76b368c0ca664dd05d08a592cfb2811944712f41)



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