Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / auto_parallel / sapp_ppb / utils / error.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-07-06 05:41 +0800

1# Copyright 2026 Huawei Technologies Co., Ltd 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================ 

15"""Error types and input validation helpers for SAPP-PPB.""" 

16from typing import Union 

17 

18Number = Union[int, float] 

19 

20 

21class SAPPError(ValueError): 

22 """Raised when SAPP-PPB detects an invalid input or configuration.""" 

23 

24 

25def assert_sapp(test: bool, msg: str) -> None: 

26 """Raise :class:`SAPPError` with ``msg`` when ``test`` is false. 

27 

28 Args: 

29 test: Condition that must hold. 

30 msg: Human-readable error message attached to the raised exception. 

31 

32 Raises: 

33 SAPPError: If ``test`` is ``False``. 

34 """ 

35 if not test: 

36 raise SAPPError(msg) 

37 

38 

39def check_in_bounds(n: Number, n_desc: str, lower_bound: Number, higher_bound: Number) -> None: 

40 """Check that ``n`` lies in the inclusive range ``[lower_bound, higher_bound]``. 

41 

42 Args: 

43 n: The value being validated. 

44 n_desc: Short description of ``n`` used in the error message. 

45 lower_bound: Lower bound (inclusive). 

46 higher_bound: Upper bound (inclusive). 

47 

48 Raises: 

49 SAPPError: If ``n`` falls outside ``[lower_bound, higher_bound]``. 

50 """ 

51 assert_sapp(n >= lower_bound, 

52 f"{n_desc} {n} should be higher than {lower_bound}") 

53 assert_sapp(n <= higher_bound, 

54 f"{n_desc} {n} should be lower than {higher_bound}")