Coverage for src / bioimageio / spec / _pretty_validation_errors.py: 44%

48 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-13 11:29 +0000

1import warnings 

2from pprint import pformat 

3from types import TracebackType 

4from typing import Any, List, Type 

5 

6from pydantic import ValidationError 

7 

8from .summary import format_loc 

9 

10try: 

11 from IPython.core.getipython import get_ipython 

12 from IPython.core.interactiveshell import InteractiveShell 

13 

14 class PrettyValidationError(ValueError): 

15 """Wrap a pydantic.ValidationError to custumize formatting.""" 

16 

17 def __init__(self, validation_error: ValidationError): 

18 super().__init__() 

19 self.error = validation_error 

20 

21 def __str__(self): 

22 errors: List[str] = [] 

23 for e in self.error.errors(include_url=False): 

24 ipt_lines = pformat( 

25 e["input"], sort_dicts=False, depth=1, compact=True, width=30 

26 ).split("\n") 

27 if len(ipt_lines) > 2: 

28 ipt_lines[1:-1] = ["..."] 

29 

30 ipt = " ".join([il.strip() for il in ipt_lines]) 

31 

32 errors.append( 

33 f"\n{format_loc(e['loc'], 'plain')}\n {e['msg']} [input={ipt}]" 

34 ) 

35 

36 return ( 

37 f"{self.error.error_count()} validation errors for" 

38 f" {self.error.title}:{''.join(errors)}" 

39 ) 

40 

41 def _custom_exception_handler( 

42 self: InteractiveShell, 

43 etype: Type[ValidationError], 

44 evalue: ValidationError, 

45 tb: TracebackType, 

46 tb_offset: Any = None, 

47 ): 

48 assert issubclass(etype, ValidationError), type(etype) 

49 assert isinstance(evalue, ValidationError), type(etype) 

50 

51 stb = self.InteractiveTB.structured_traceback( # pyright: ignore 

52 etype, PrettyValidationError(evalue), tb, tb_offset=tb_offset 

53 ) 

54 

55 if isinstance(stb, list): 

56 stb_clean = [] 

57 for line in stb: # pyright: ignore[reportUnknownVariableType] 

58 if ( 

59 isinstance(line, str) 

60 and "pydantic" in line 

61 and "__tracebackhide__" in line 

62 ): 

63 # ignore pydantic internal frame in traceback 

64 continue 

65 stb_clean.append(line) 

66 

67 stb = stb_clean 

68 

69 self._showtraceback(etype, PrettyValidationError(evalue), stb) # type: ignore 

70 

71 def _enable_pretty_validation_errors_in_ipynb(): 

72 """A modestly hacky way to display prettified validaiton error messages and traceback 

73 in interactive Python notebooks""" 

74 ipy = get_ipython() 

75 if ipy is not None: 

76 ipy.set_custom_exc((ValidationError,), _custom_exception_handler) 

77 

78except ImportError: 

79 _enabled = False 

80else: 

81 try: 

82 _enable_pretty_validation_errors_in_ipynb() 

83 except Exception as e: 

84 _enabled = False 

85 warnings.warn( 

86 "Failed to enable pretty validation errors in ipython: " + str(e), 

87 stacklevel=2, 

88 ) 

89 else: 

90 _enabled = True 

91 

92PRETTY_VALIDATION_ERRORS_IN_IPYNB_ENABLED = _enabled 

93"""Whether pretty validation errors in IPython notebooks were successfully enabled during import."""