Coverage for src / chebpy / exceptions.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-22 21:33 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-22 21:33 +0000
1"""Exception classes for the ChebPy package.
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"""
8from abc import ABC, abstractmethod
9from typing import Any
12class ChebpyBaseError(Exception, ABC):
13 """Abstract base class for all ChebPy exceptions.
15 This class serves as the base for all exception types in the ChebPy package.
16 It provides a common interface for exception handling and requires subclasses
17 to define a default error message.
19 Attributes:
20 message (str): The error message to be displayed.
21 """
23 def __init__(self, *args: Any) -> None:
24 """Initialize the exception with an optional custom message.
26 Args:
27 *args: Variable length argument list. If provided, the first argument
28 is used as the error message. Otherwise, the default message is used.
29 """
30 if args:
31 self.message = args[0]
32 else:
33 self.message = self.default_message
35 def __str__(self) -> str:
36 """Return the string representation of the exception.
38 Returns:
39 str: The error message.
40 """
41 return str(self.message)
43 @property
44 @abstractmethod
45 def default_message(self) -> str:
46 """Default error message for the exception.
48 This property must be implemented by all concrete subclasses.
50 Returns:
51 str: The default error message.
53 Raises:
54 NotImplementedError: If the subclass does not implement this property.
55 """
56 raise NotImplementedError
59# ===============================================
60# chebpy.utilities.Interval exceptions
61# ===============================================
63# Exception raised when two intervals overlap but should be disjoint
64IntervalOverlap = type(
65 "IntervalOverlap",
66 (ChebpyBaseError,),
67 {
68 "default_message": "The supplied Interval objects overlap",
69 "__doc__": """Exception raised when intervals overlap.
71 This exception is raised when two or more intervals overlap
72 but are required to be disjoint for the operation.
73 """,
74 },
75)
77# Exception raised when intervals have gaps between them
78IntervalGap = type(
79 "IntervalGap",
80 (ChebpyBaseError,),
81 {
82 "default_message": "The supplied Interval objects do not form a complete partition "
83 "of the approximation interval",
84 "__doc__": """Exception raised when intervals have gaps.
86 This exception is raised when a collection of intervals does not
87 form a complete partition of the approximation interval.
88 """,
89 },
90)
92# Exception raised when intervals don't match for an operation
93IntervalMismatch = type(
94 "IntervalMismatch",
95 (ChebpyBaseError,),
96 {
97 "default_message": "This operation can only be performed for Fun objects defined on identical intervals",
98 "__doc__": """Exception raised when intervals don't match.
100 This exception is raised when an operation requires Fun objects
101 to be defined on identical intervals, but they are not.
102 """,
103 },
104)
106# Exception raised when an interval is not a subinterval of another
107NotSubinterval = type(
108 "NotSubinterval",
109 (ChebpyBaseError,),
110 {
111 "default_message": "Not a subinterval",
112 "__doc__": """Exception raised when an interval is not a subinterval.
114 This exception is raised when an interval is expected to be
115 a subinterval of another interval, but it is not.
116 """,
117 },
118)
120# Exception raised when interval values are not strictly increasing
121IntervalValues = type(
122 "IntervalValues",
123 (ChebpyBaseError,),
124 {
125 "default_message": "The defining values of a Interval object must be strictly increasing",
126 "__doc__": """Exception raised when interval values are invalid.
128 This exception is raised when the defining values of an Interval
129 object are not strictly increasing.
130 """,
131 },
132)
135# ===============================================
136# chebpy.utilities.Domain exceptions
137# ===============================================
139# Exception raised when a domain is invalid
140InvalidDomain = type(
141 "InvalidDomain",
142 (ChebpyBaseError,),
143 {
144 "default_message": "Domain objects must be initialised from an iterable "
145 "collection of at least two monotonically increasing "
146 "scalars",
147 "__doc__": """Exception raised when a domain is invalid.
149 This exception is raised when attempting to create a Domain object
150 with invalid parameters, such as non-monotonic values or too few points.
151 """,
152 },
153)
155# Exception raised when a domain is not a subdomain of another
156NotSubdomain = type(
157 "NotSubdomain",
158 (ChebpyBaseError,),
159 {
160 "default_message": "The support of the target Domain object is required "
161 "to define a subinterval of the support of the "
162 "original",
163 "__doc__": """Exception raised when a domain is not a subdomain.
165 This exception is raised when a domain is expected to be
166 a subdomain of another domain, but it is not.
167 """,
168 },
169)
171# Exception raised when supports don't match for an operation
172SupportMismatch = type(
173 "SupportMismatch",
174 (ChebpyBaseError,),
175 {
176 "default_message": "Both objects are required to be supported on the same interval",
177 "__doc__": """Exception raised when supports don't match.
179 This exception is raised when an operation requires objects
180 to be supported on the same interval, but they are not.
181 """,
182 },
183)
185# Exception raised when the length argument for a function is invalid
186BadFunLengthArgument = type(
187 "BadFunLengthArgument",
188 (ChebpyBaseError,),
189 {
190 "default_message": "The 'n' argument must be either a single numeric "
191 "value, or iterable thereof posessing one fewer "
192 "elements than the size of the domain",
193 "__doc__": """Exception raised when a function length argument is invalid.
195 This exception is raised when the 'n' argument for a function does not
196 meet the requirements: it must be either a single numeric value or an
197 iterable with one fewer elements than the size of the domain.
198 """,
199 },
200)