Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/build/lib/airflow/serialization/serializers/bignum.py: 43%

23 statements  

« 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 

19 

20from decimal import Decimal 

21from typing import TYPE_CHECKING 

22 

23from airflow.utils.module_loading import qualname 

24 

25if TYPE_CHECKING: 

26 from airflow.serialization.serde import U 

27 

28 

29serializers = [Decimal] 

30deserializers = serializers 

31 

32__version__ = 1 

33 

34 

35def serialize(o: object) -> tuple[U, str, int, bool]: 

36 if isinstance(o, Decimal): 

37 name = qualname(o) 

38 _, _, exponent = o.as_tuple() 

39 if exponent >= 0: # No digits after the decimal point. 

40 return int(o), name, __version__, True 

41 # Technically lossy due to floating point errors, but the best we 

42 # can do without implementing a custom encode function. 

43 return float(o), name, __version__, True 

44 

45 return "", "", 0, False 

46 

47 

48def deserialize(classname: str, version: int, data: object) -> Decimal: 

49 if version > __version__: 

50 raise TypeError(f"serialized {version} of {classname} > {__version__}") 

51 

52 if classname != qualname(Decimal): 

53 raise TypeError(f"{classname} != {qualname(Decimal)}") 

54 

55 return Decimal(str(data))