Coverage for glotter/errors.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-08 17:57 +0000

1from typing import List, NoReturn, Optional 

2 

3from pydantic import ValidationError 

4from pydantic_core import InitErrorDetails, PydanticCustomError 

5 

6 

7def get_error_details(msg: str, loc: tuple, input) -> InitErrorDetails: 

8 return InitErrorDetails( 

9 type=PydanticCustomError("value_error", msg), 

10 loc=loc, 

11 input=input, 

12 ) 

13 

14 

15def raise_simple_validation_error(cls, msg, input, loc: Optional[tuple] = None) -> NoReturn: 

16 raise ValidationError.from_exception_data( 

17 title=cls.__name__, 

18 line_errors=[get_error_details(msg, loc or (), input)], 

19 ) 

20 

21 

22def raise_validation_errors(cls, errors: List[InitErrorDetails]) -> NoReturn: 

23 raise ValidationError.from_exception_data(title=cls.__name__, line_errors=errors) 

24 

25 

26def validate_str_list( 

27 cls, values, item_loc: Optional[tuple] = None, raise_exc: bool = True 

28) -> List[InitErrorDetails]: 

29 loc = item_loc or () 

30 errors = [] 

31 if not isinstance(values, list): 

32 errors += [get_error_details("Input should be a valid list", loc, values)] 

33 else: 

34 errors += [ 

35 get_error_details("Input should be a valid string", loc + (index,), value) 

36 for index, value in enumerate(values) 

37 if not isinstance(value, str) 

38 ] 

39 

40 if errors and raise_exc: 

41 raise_validation_errors(cls, errors) 

42 

43 return errors