Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/src/zserio/compare.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2026-10-19 14:18 +0000

1""" 

2The module implements ordering helpers used by the less than operator of the generated objects. 

3""" 

4 

5import typing 

6 

7 

8def compare(lhs: typing.Any, rhs: typing.Any) -> int: 

9 """ 

10 Compares two values. 

11 

12 A value which is not set (None) is less than a value which is set. Generated objects and arrays of the same 

13 type are compared by their three-way method _zserio_cmp, which visits each nested value only once. Other 

14 values are compared using only the less than operator. 

15 

16 :param lhs: Left hand side value. 

17 :param rhs: Right hand side value. 

18 

19 :returns: Negative number when lhs is less than rhs, positive number when rhs is less than lhs, else zero. 

20 """ 

21 

22 if lhs is None: 

23 return 0 if rhs is None else -1 

24 if rhs is None: 

25 return 1 

26 if type(lhs) is type(rhs) and hasattr(lhs, "_zserio_cmp"): 

27 return lhs._zserio_cmp(rhs) # pylint: disable=protected-access 

28 if lhs < rhs: 

29 return -1 

30 if rhs < lhs: 

31 return 1 

32 

33 return 0