bolkedebruin commented on code in PR #28067: URL: https://github.com/apache/airflow/pull/28067#discussion_r1041038649
########## airflow/utils/serialization.py: ########## @@ -0,0 +1,290 @@ +# +# 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 __future__ import annotations + +import dataclasses +import enum +import logging +import re +import sys +from importlib import import_module +from types import ModuleType +from typing import Any, TypeVar, Union + +import attr + +import airflow.utils.serializers +from airflow.configuration import conf +from airflow.utils.module_loading import import_string, iter_namespace, qualname + +log = logging.getLogger(__name__) + +MAX_RECURSION_DEPTH = sys.getrecursionlimit() - 1 + +CLASSNAME = "__classname__" +VERSION = "__version__" +DATA = "__data__" +SCHEMA_ID = "__id__" +CACHE = "__cache__" + +OLD_TYPE = "__type" +OLD_SOURCE = "__source" +OLD_DATA = "__var" + +DEFAULT_VERSION = 0 + +T = TypeVar("T", bool, float, int, dict, list, str, tuple, set) +U = Union[bool, float, int, dict, list, str, tuple, set] +S = Union[list, tuple, set] + +_serializers: dict[str, ModuleType] = {} +_deserializers: dict[str, ModuleType] = {} +_extra_allowed: set[str] = set() + +_primitives = (int, bool, float, str) +_iterables = (list, set, tuple) +_patterns: list[re.Pattern] = [] + +_reverse_cache: dict[int, tuple[ModuleType, str, int]] = {} + + +def encode(cls: str, version: int, data: T) -> dict[str, str | int | T]: + """Encodes o so it can be understood by the deserializer""" + return {CLASSNAME: cls, VERSION: version, DATA: data} + + +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: + """ + Recursively serializes objects into a primitive. Primitives (int, float, int, bool) + are returned as is. Tuples and dicts are iterated over, where it is assumed that keys + for dicts can be represented as str. Values that are not primitives are serialized if + a serializer is found for them. The order in which serializers are used + is 1) a serialize function provided by the object 2) a registered serializer in + the namespace of airflow.utils.serializers and 3) an attr or dataclass annotations. + If a serializer cannot be found a TypeError is raised. + + :param o: object to serialize + :param depth: private + :return: a primitive + """ + if depth == MAX_RECURSION_DEPTH: + raise RecursionError("maximum recursion depth reached for serialization") + + # None remains None + if o is None: + return o + + # primitive types are returned as is + if isinstance(o, _primitives): + if isinstance(o, enum.IntEnum): + return o.value + + return o + + # tuples and plain dicts are iterated over recursively + if isinstance(o, _iterables): + return [serialize(d, depth + 1) for d in o] + + if isinstance(o, dict): + if CLASSNAME in o or SCHEMA_ID in o: + raise AttributeError(f"reserved key {CLASSNAME} or {SCHEMA_ID} found in dict to serialize") + + return {str(k): serialize(v, depth + 1) for k, v in o.items()} + + cls = type(o) + qn = qualname(o) + + # custom serializers + dct = { + CLASSNAME: qn, + VERSION: getattr(cls, "__version__", DEFAULT_VERSION), Review Comment: Yes it was shipped not nowhere implemented, so only custom classes after that ship this. Some of the provider collide with `version` (gcs) so we are obtaining the wrong version then. SO I'd prefer to take the small hit. -- 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]
