Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/build/lib/airflow/serialization/serializers/numpy.py: 44%
32 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +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 qualname
24serializers = []
26try:
27 import numpy as np
29 serializers = [
30 np.int_,
31 np.intc,
32 np.intp,
33 np.int8,
34 np.int16,
35 np.int32,
36 np.int64,
37 np.uint8,
38 np.uint16,
39 np.uint32,
40 np.uint64,
41 np.bool_,
42 np.float_,
43 np.float16,
44 np.float64,
45 np.complex_,
46 np.complex64,
47 np.complex128,
48 ]
49except ImportError:
50 np = None # type: ignore
53if TYPE_CHECKING:
54 from airflow.serialization.serde import U
56deserializers: list = serializers
57_deserializers: dict[str, type[object]] = {qualname(x): x for x in deserializers}
59__version__ = 1
62def serialize(o: object) -> tuple[U, str, int, bool]:
63 if np is None:
64 return "", "", 0, False
66 name = qualname(o)
67 if isinstance(
68 o,
69 (
70 np.int_,
71 np.intc,
72 np.intp,
73 np.int8,
74 np.int16,
75 np.int32,
76 np.int64,
77 np.uint8,
78 np.uint16,
79 np.uint32,
80 np.uint64,
81 ),
82 ):
83 return int(o), name, __version__, True
85 if isinstance(o, np.bool_):
86 return bool(np), name, __version__, True
88 if isinstance(
89 o, (np.float_, np.float16, np.float32, np.float64, np.complex_, np.complex64, np.complex128)
90 ):
91 return float(o), name, __version__, True
93 return "", "", 0, False
96def deserialize(classname: str, version: int, data: str) -> Any:
97 if version > __version__:
98 raise TypeError("serialized version is newer than class version")
100 f = _deserializers.get(classname, None)
101 if callable(f):
102 return f(data) # type: ignore [call-arg]
104 raise TypeError(f"unsupported {classname} found for numpy deserialization")