Coverage for transformer_lens/tools/model_registry/exceptions.py: 41%

36 statements  

« prev     ^ index     » next       coverage.py v7.10.1, created at 2026-04-30 01:33 +0000

1"""Custom exceptions for the model registry API. 

2 

3This module defines specific exceptions that can be raised by the model registry 

4to provide clear error messages for common failure scenarios. 

5""" 

6 

7 

8class ModelRegistryError(Exception): 

9 """Base exception for all model registry errors.""" 

10 

11 pass 

12 

13 

14class ModelNotFoundError(ModelRegistryError): 

15 """Raised when a requested model ID is not found in the registry. 

16 

17 Attributes: 

18 model_id: The model ID that was not found 

19 suggestion: Optional suggested alternative model 

20 """ 

21 

22 def __init__(self, model_id: str, suggestion: str | None = None): 

23 self.model_id = model_id 

24 self.suggestion = suggestion 

25 msg = f"Model '{model_id}' not found in the registry" 

26 if suggestion: 26 ↛ 27line 26 didn't jump to line 27 because the condition on line 26 was never true

27 msg += f". Did you mean '{suggestion}'?" 

28 super().__init__(msg) 

29 

30 

31class ArchitectureNotSupportedError(ModelRegistryError): 

32 """Raised when an architecture is not supported by TransformerLens. 

33 

34 Attributes: 

35 architecture_id: The architecture that is not supported 

36 model_count: Number of models using this architecture (if known) 

37 """ 

38 

39 def __init__(self, architecture_id: str, model_count: int | None = None): 

40 self.architecture_id = architecture_id 

41 self.model_count = model_count 

42 msg = f"Architecture '{architecture_id}' is not supported by TransformerLens" 

43 if model_count is not None: 

44 msg += f" ({model_count} models on HuggingFace use this architecture)" 

45 super().__init__(msg) 

46 

47 

48class DataNotLoadedError(ModelRegistryError): 

49 """Raised when registry data has not been loaded or is unavailable. 

50 

51 Attributes: 

52 data_type: Type of data that was not loaded (e.g., "supported_models") 

53 path: Optional path where data was expected 

54 """ 

55 

56 def __init__(self, data_type: str, path: str | None = None): 

57 self.data_type = data_type 

58 self.path = path 

59 msg = f"Registry data '{data_type}' has not been loaded" 

60 if path: 

61 msg += f" (expected at: {path})" 

62 super().__init__(msg) 

63 

64 

65class DataValidationError(ModelRegistryError): 

66 """Raised when registry data fails validation. 

67 

68 Attributes: 

69 file_path: Path to the file that failed validation 

70 errors: List of validation error messages 

71 """ 

72 

73 def __init__(self, file_path: str, errors: list[str]): 

74 self.file_path = file_path 

75 self.errors = errors 

76 msg = f"Data validation failed for '{file_path}': {len(errors)} errors" 

77 if errors: 

78 msg += f"\n - " + "\n - ".join(errors[:5]) 

79 if len(errors) > 5: 

80 msg += f"\n ... and {len(errors) - 5} more" 

81 super().__init__(msg)