builtin.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. from typing import TYPE_CHECKING, cast
  20. from airflow.utils.module_loading import qualname
  21. if TYPE_CHECKING:
  22. from airflow.serialization.serde import U
  23. __version__ = 1
  24. serializers = ["builtins.frozenset", "builtins.set", "builtins.tuple"]
  25. deserializers = serializers
  26. stringifiers = serializers
  27. def serialize(o: object) -> tuple[U, str, int, bool]:
  28. return list(cast(list, o)), qualname(o), __version__, True
  29. def deserialize(classname: str, version: int, data: list) -> tuple | set | frozenset:
  30. if version > __version__:
  31. raise TypeError("serialized version is newer than class version")
  32. if classname == qualname(tuple):
  33. return tuple(data)
  34. if classname == qualname(set):
  35. return set(data)
  36. if classname == qualname(frozenset):
  37. return frozenset(data)
  38. raise TypeError(f"do not know how to deserialize {classname}")
  39. def stringify(classname: str, version: int, data: list) -> str:
  40. if classname not in stringifiers:
  41. raise TypeError(f"do not know how to stringify {classname}")
  42. s = ",".join(str(d) for d in data)
  43. return f"({s})"