Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/airflow/serialization/serializers/numpy.py: 35%
26 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
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.
18from __future__ import annotations
20from typing import TYPE_CHECKING, Any
22from airflow.utils.module_loading import import_string, qualname
24# lazy loading for performance reasons
25serializers = [
26 "numpy.int8",
27 "numpy.int16",
28 "numpy.int32",
29 "numpy.int64",
30 "numpy.uint8",
31 "numpy.uint16",
32 "numpy.uint32",
33 "numpy.uint64",
34 "numpy.bool_",
35 "numpy.float64",
36 "numpy.float16",
37 "numpy.complex128",
38 "numpy.complex64",
39]
41if TYPE_CHECKING:
42 from airflow.serialization.serde import U
44deserializers = serializers
46__version__ = 1
49def serialize(o: object) -> tuple[U, str, int, bool]:
50 import numpy as np
52 if np is None:
53 return "", "", 0, False
55 name = qualname(o)
56 if isinstance(
57 o,
58 (
59 np.int_,
60 np.intc,
61 np.intp,
62 np.int8,
63 np.int16,
64 np.int32,
65 np.int64,
66 np.uint8,
67 np.uint16,
68 np.uint32,
69 np.uint64,
70 ),
71 ):
72 return int(o), name, __version__, True
74 if isinstance(o, np.bool_):
75 return bool(np), name, __version__, True
77 if isinstance(
78 o, (np.float_, np.float16, np.float32, np.float64, np.complex_, np.complex64, np.complex128)
79 ):
80 return float(o), name, __version__, True
82 return "", "", 0, False
85def deserialize(classname: str, version: int, data: str) -> Any:
86 if version > __version__:
87 raise TypeError("serialized version is newer than class version")
89 if classname not in deserializers:
90 raise TypeError(f"unsupported {classname} found for numpy deserialization")
92 return import_string(classname)(data)