Coverage for chebpy/core/exceptions.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-07 10:30 +0000

1"""Exception classes for the ChebPy package. 

2 

3This module defines the exception hierarchy used throughout the ChebPy package. 

4It includes a base exception class and various specific exception types for 

5different error conditions related to intervals, domains, and function operations. 

6""" 

7 

8from abc import ABC, abstractmethod 

9 

10 

11class ChebpyBaseError(Exception, ABC): 

12 """Abstract base class for all ChebPy exceptions. 

13 

14 This class serves as the base for all exception types in the ChebPy package. 

15 It provides a common interface for exception handling and requires subclasses 

16 to define a default error message. 

17 

18 Attributes: 

19 message (str): The error message to be displayed. 

20 """ 

21 

22 def __init__(self, *args): 

23 """Initialize the exception with an optional custom message. 

24 

25 Args: 

26 *args: Variable length argument list. If provided, the first argument 

27 is used as the error message. Otherwise, the default message is used. 

28 """ 

29 if args: 

30 self.message = args[0] 

31 else: 

32 self.message = self.default_message 

33 

34 def __str__(self): 

35 """Return the string representation of the exception. 

36 

37 Returns: 

38 str: The error message. 

39 """ 

40 return self.message 

41 

42 @property 

43 @abstractmethod 

44 def default_message(self): 

45 """Default error message for the exception. 

46 

47 This property must be implemented by all concrete subclasses. 

48 

49 Returns: 

50 str: The default error message. 

51 

52 Raises: 

53 NotImplementedError: If the subclass does not implement this property. 

54 """ 

55 raise NotImplementedError 

56 

57 

58# =============================================== 

59# chebpy.core.utilities.Interval exceptions 

60# =============================================== 

61 

62# Exception raised when two intervals overlap but should be disjoint 

63IntervalOverlap = type( 

64 "IntervalOverlap", 

65 (ChebpyBaseError,), 

66 { 

67 "default_message": "The supplied Interval objects overlap", 

68 "__doc__": """Exception raised when intervals overlap. 

69 

70 This exception is raised when two or more intervals overlap 

71 but are required to be disjoint for the operation. 

72 """, 

73 }, 

74) 

75 

76# Exception raised when intervals have gaps between them 

77IntervalGap = type( 

78 "IntervalGap", 

79 (ChebpyBaseError,), 

80 { 

81 "default_message": "The supplied Interval objects do not form a complete partition " 

82 "of the approximation interval", 

83 "__doc__": """Exception raised when intervals have gaps. 

84 

85 This exception is raised when a collection of intervals does not 

86 form a complete partition of the approximation interval. 

87 """, 

88 }, 

89) 

90 

91# Exception raised when intervals don't match for an operation 

92IntervalMismatch = type( 

93 "IntervalMismatch", 

94 (ChebpyBaseError,), 

95 { 

96 "default_message": "This operation can only be performed for Fun objects defined on identical intervals", 

97 "__doc__": """Exception raised when intervals don't match. 

98 

99 This exception is raised when an operation requires Fun objects 

100 to be defined on identical intervals, but they are not. 

101 """, 

102 }, 

103) 

104 

105# Exception raised when an interval is not a subinterval of another 

106NotSubinterval = type( 

107 "NotSubinterval", 

108 (ChebpyBaseError,), 

109 { 

110 "default_message": "Not a subinterval", 

111 "__doc__": """Exception raised when an interval is not a subinterval. 

112 

113 This exception is raised when an interval is expected to be 

114 a subinterval of another interval, but it is not. 

115 """, 

116 }, 

117) 

118 

119# Exception raised when interval values are not strictly increasing 

120IntervalValues = type( 

121 "IntervalValues", 

122 (ChebpyBaseError,), 

123 { 

124 "default_message": "The defining values of a Interval object must be strictly increasing", 

125 "__doc__": """Exception raised when interval values are invalid. 

126 

127 This exception is raised when the defining values of an Interval 

128 object are not strictly increasing. 

129 """, 

130 }, 

131) 

132 

133 

134# =============================================== 

135# chebpy.core.utilities.Domain exceptions 

136# =============================================== 

137 

138# Exception raised when a domain is invalid 

139InvalidDomain = type( 

140 "InvalidDomain", 

141 (ChebpyBaseError,), 

142 { 

143 "default_message": "Domain objects must be initialised from an iterable " 

144 "collection of at least two monotonically increasing " 

145 "scalars", 

146 "__doc__": """Exception raised when a domain is invalid. 

147 

148 This exception is raised when attempting to create a Domain object 

149 with invalid parameters, such as non-monotonic values or too few points. 

150 """, 

151 }, 

152) 

153 

154# Exception raised when a domain is not a subdomain of another 

155NotSubdomain = type( 

156 "NotSubdomain", 

157 (ChebpyBaseError,), 

158 { 

159 "default_message": "The support of the target Domain object is required " 

160 "to define a subinterval of the support of the " 

161 "original", 

162 "__doc__": """Exception raised when a domain is not a subdomain. 

163 

164 This exception is raised when a domain is expected to be 

165 a subdomain of another domain, but it is not. 

166 """, 

167 }, 

168) 

169 

170# Exception raised when supports don't match for an operation 

171SupportMismatch = type( 

172 "SupportMismatch", 

173 (ChebpyBaseError,), 

174 { 

175 "default_message": "Both objects are required to be supported on the same interval", 

176 "__doc__": """Exception raised when supports don't match. 

177 

178 This exception is raised when an operation requires objects 

179 to be supported on the same interval, but they are not. 

180 """, 

181 }, 

182) 

183 

184# Exception raised when the length argument for a function is invalid 

185BadFunLengthArgument = type( 

186 "BadFunLengthArgument", 

187 (ChebpyBaseError,), 

188 { 

189 "default_message": "The 'n' argument must be either a single numeric " 

190 "value, or iterable thereof posessing one fewer " 

191 "elements than the size of the domain", 

192 "__doc__": """Exception raised when a function length argument is invalid. 

193 

194 This exception is raised when the 'n' argument for a function does not 

195 meet the requirements: it must be either a single numeric value or an 

196 iterable with one fewer elements than the size of the domain. 

197 """, 

198 }, 

199)