Coverage for src/_griffe/exceptions.py: 89.66%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-15 16:47 +0200

1# This module contains all the exceptions specific to Griffe. 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7if TYPE_CHECKING: 

8 from _griffe.models import Alias 

9 

10 

11class GriffeError(Exception): 

12 """The base exception for all Griffe errors.""" 

13 

14 

15class LoadingError(GriffeError): 

16 """The base exception for all Griffe errors.""" 

17 

18 

19class NameResolutionError(GriffeError): 

20 """Exception for names that cannot be resolved in a object scope.""" 

21 

22 

23class UnhandledEditableModuleError(GriffeError): 

24 """Exception for unhandled editables modules, when searching modules.""" 

25 

26 

27class UnimportableModuleError(GriffeError): 

28 """Exception for modules that cannot be imported.""" 

29 

30 

31class AliasResolutionError(GriffeError): 

32 """Exception for alias that cannot be resolved.""" 

33 

34 def __init__(self, alias: Alias) -> None: 

35 """Initialize the exception. 

36 

37 Parameters: 

38 alias: The alias that could not be resolved. 

39 """ 

40 self.alias: Alias = alias 

41 """The alias that triggered the error.""" 

42 

43 message = f"Could not resolve alias {alias.path} pointing at {alias.target_path}" 

44 try: 

45 filepath = alias.parent.relative_filepath # type: ignore[union-attr] 

46 except BuiltinModuleError: 

47 pass 

48 else: 

49 message += f" (in {filepath}:{alias.alias_lineno})" 

50 super().__init__(message) 

51 

52 

53class CyclicAliasError(GriffeError): 

54 """Exception raised when a cycle is detected in aliases.""" 

55 

56 def __init__(self, chain: list[str]) -> None: 

57 """Initialize the exception. 

58 

59 Parameters: 

60 chain: The cyclic chain of items (such as target path). 

61 """ 

62 self.chain: list[str] = chain 

63 """The chain of aliases that created the cycle.""" 

64 

65 super().__init__("Cyclic aliases detected:\n " + "\n ".join(self.chain)) 

66 

67 

68class LastNodeError(GriffeError): 

69 """Exception raised when trying to access a next or previous node.""" 

70 

71 

72class RootNodeError(GriffeError): 

73 """Exception raised when trying to use siblings properties on a root node.""" 

74 

75 

76class BuiltinModuleError(GriffeError): 

77 """Exception raised when trying to access the filepath of a builtin module.""" 

78 

79 

80class ExtensionError(GriffeError): 

81 """Base class for errors raised by extensions.""" 

82 

83 

84class ExtensionNotLoadedError(ExtensionError): 

85 """Exception raised when an extension could not be loaded.""" 

86 

87 

88class GitError(GriffeError): 

89 """Exception raised for errors related to Git."""