From 693f3453ccca48dd42ebbba54cdbe8a456b13fe4 Mon Sep 17 00:00:00 2001 From: Kevin Yang <85313829+sjyangkevin@users.noreply.github.com> Date: Sun, 19 Oct 2025 13:38:25 -0400 Subject: [PATCH] [v3-1-test] update serializer document to reflect the latest change in codebase (#56269) (cherry picked from commit c2bccf13b001ad4914e6dc27663a109eb0fb214f) Co-authored-by: Kevin Yang <85313829+sjyangkevin@users.noreply.github.com> --- .../authoring-and-scheduling/serializers.rst | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/airflow-core/docs/authoring-and-scheduling/serializers.rst b/airflow-core/docs/authoring-and-scheduling/serializers.rst index 433485bb3b8af..fb19a111d9177 100644 --- a/airflow-core/docs/authoring-and-scheduling/serializers.rst +++ b/airflow-core/docs/authoring-and-scheduling/serializers.rst @@ -74,8 +74,7 @@ Airflow Object @staticmethod def deserialize(data: dict[str, Any], version: int): - f = Foo(a=data["a"]) - f.b = data["b"] + f = Foo(a=data["a"], v=data["b"]) return f @@ -86,17 +85,18 @@ Registered from __future__ import annotations - from decimal import Decimal from typing import TYPE_CHECKING from airflow.utils.module_loading import qualname if TYPE_CHECKING: + import decimal + from airflow.serialization.serde import U serializers = [ - Decimal + "decimal.Decimal" ] # this can be a type or a fully qualified str. Str can be used to prevent circular imports deserializers = serializers # in some cases you might not have a deserializer (e.g. k8s pod) @@ -105,25 +105,28 @@ Registered # the serializer expects output, classname, version, is_serialized? def serialize(o: object) -> tuple[U, str, int, bool]: - if isinstance(o, Decimal): - name = qualname(o) - _, _, exponent = o.as_tuple() - if exponent >= 0: # No digits after the decimal point. - return int(o), name, __version__, True - # Technically lossy due to floating point errors, but the best we - # can do without implementing a custom encode function. - return float(o), name, __version__, True + from decimal import Decimal - return "", "", 0, False + if not isinstance(o, Decimal): + return "", "", 0, False + name = qualname(o) + _, _, exponent = o.as_tuple() + if isinstance(exponent, int) and exponent >= 0: # No digits after the decimal point. + return int(o), name, __version__, True + # Technically lossy due to floating point errors, but the best we + # can do without implementing a custom encode function. + return float(o), name, __version__, True # the deserializer sanitizes the data for you, so you do not need to deserialize values yourself - def deserialize(classname: str, version: int, data: object) -> Decimal: + def deserialize(cls: type, version: int, data: object) -> Decimal: + from decimal import Decimal + # always check version compatibility if version > __version__: - raise TypeError(f"serialized {version} of {classname} > {__version__}") + raise TypeError(f"serialized {version} of {qualname(cls)} > {__version__}") - if classname != qualname(Decimal): - raise TypeError(f"{classname} != {qualname(Decimal)}") + if cls is not Decimal: + raise TypeError(f"do not know how to deserialize {qualname(cls)}") return Decimal(str(data))