1111from __future__ import annotations
1212
1313import typing as ty
14- from dataclasses import dataclass , fields
1514
1615OFFSET_ERROR_DOCS_HTML = "https://pint.readthedocs.io/en/stable/user/nonmult.html"
1716LOG_ERROR_DOCS_HTML = "https://pint.readthedocs.io/en/stable/user/log_units.html"
@@ -81,82 +80,87 @@ def def_err(self, msg: str):
8180 return DefinitionError (self .name , self .__class__ , msg )
8281
8382
84- @dataclass (frozen = False )
8583class PintError (Exception ):
8684 """Base exception for all Pint errors."""
8785
8886
89- @dataclass (frozen = False )
9087class DefinitionError (ValueError , PintError ):
9188 """Raised when a definition is not properly constructed."""
9289
9390 name : str
9491 definition_type : type
9592 msg : str
9693
94+ def __init__ (self , name : str , definition_type : type , msg : str ):
95+ self .name = name
96+ self .definition_type = definition_type
97+ self .msg = msg
98+
9799 def __str__ (self ):
98100 msg = f"Cannot define '{ self .name } ' ({ self .definition_type } ): { self .msg } "
99101 return msg
100102
101103 def __reduce__ (self ):
102- return self .__class__ , tuple ( getattr ( self , f . name ) for f in fields ( self ) )
104+ return self .__class__ , ( self . name , self . definition_type , self . msg )
103105
104106
105- @dataclass (frozen = False )
106107class DefinitionSyntaxError (ValueError , PintError ):
107108 """Raised when a textual definition has a syntax error."""
108109
109110 msg : str
110111
112+ def __init__ (self , msg : str ):
113+ self .msg = msg
114+
111115 def __str__ (self ):
112116 return self .msg
113117
114118 def __reduce__ (self ):
115- return self .__class__ , tuple ( getattr ( self , f . name ) for f in fields ( self ) )
119+ return self .__class__ , ( self . msg , )
116120
117121
118- @dataclass (frozen = False )
119122class RedefinitionError (ValueError , PintError ):
120123 """Raised when a unit or prefix is redefined."""
121124
122125 name : str
123126 definition_type : type
124127
128+ def __init__ (self , name : str , definition_type : type ):
129+ self .name = name
130+ self .definition_type = definition_type
131+
125132 def __str__ (self ):
126133 msg = f"Cannot redefine '{ self .name } ' ({ self .definition_type } )"
127134 return msg
128135
129136 def __reduce__ (self ):
130- return self .__class__ , tuple ( getattr ( self , f .name ) for f in fields ( self ) )
137+ return self .__class__ , ( self .name , self . definition_type )
131138
132139
133- @dataclass (frozen = False )
134140class UndefinedUnitError (AttributeError , PintError ):
135141 """Raised when the units are not defined in the unit registry."""
136142
137- unit_names : str | tuple [str , ...]
143+ unit_names : tuple [str , ...]
144+
145+ def __init__ (self , unit_names : str | ty .Iterable [str ]):
146+ if isinstance (unit_names , str ):
147+ self .unit_names = (unit_names ,)
148+ else :
149+ self .unit_names = tuple (unit_names )
138150
139151 def __str__ (self ):
140- if isinstance (self .unit_names , str ):
141- return f"'{ self .unit_names } ' is not defined in the unit registry"
142- if (
143- isinstance (self .unit_names , (tuple , list , set ))
144- and len (self .unit_names ) == 1
145- ):
152+ if len (self .unit_names ) == 1 :
146153 return f"'{ tuple (self .unit_names )[0 ]} ' is not defined in the unit registry"
147154 return f"{ tuple (self .unit_names )} are not defined in the unit registry"
148155
149156 def __reduce__ (self ):
150- return self .__class__ , tuple ( getattr ( self , f . name ) for f in fields ( self ) )
157+ return self .__class__ , ( self . unit_names , )
151158
152159
153- @dataclass (frozen = False )
154160class PintTypeError (TypeError , PintError ):
155- def __reduce__ (self ):
156- return self .__class__ , tuple (getattr (self , f .name ) for f in fields (self ))
161+ pass
157162
158163
159- @dataclass (frozen = False )
160164class DimensionalityError (PintTypeError ):
161165 """Raised when trying to convert between incompatible units."""
162166
@@ -166,6 +170,20 @@ class DimensionalityError(PintTypeError):
166170 dim2 : str = ""
167171 extra_msg : str = ""
168172
173+ def __init__ (
174+ self ,
175+ units1 : ty .Any ,
176+ units2 : ty .Any ,
177+ dim1 : str = "" ,
178+ dim2 : str = "" ,
179+ extra_msg : str = "" ,
180+ ) -> None :
181+ self .units1 = units1
182+ self .units2 = units2
183+ self .dim1 = dim1
184+ self .dim2 = dim2
185+ self .extra_msg = extra_msg
186+
169187 def __str__ (self ):
170188 if self .dim1 or self .dim2 :
171189 dim1 = f" ({ self .dim1 } )"
@@ -180,16 +198,25 @@ def __str__(self):
180198 )
181199
182200 def __reduce__ (self ):
183- return self .__class__ , tuple (getattr (self , f .name ) for f in fields (self ))
201+ return self .__class__ , (
202+ self .units1 ,
203+ self .units2 ,
204+ self .dim1 ,
205+ self .dim2 ,
206+ self .extra_msg ,
207+ )
184208
185209
186- @dataclass (frozen = False )
187210class OffsetUnitCalculusError (PintTypeError ):
188211 """Raised on ambiguous operations with offset units."""
189212
190213 units1 : ty .Any
191214 units2 : ty .Optional [ty .Any ] = None
192215
216+ def __init__ (self , units1 : ty .Any , units2 : ty .Optional [ty .Any ] = None ) -> None :
217+ self .units1 = units1
218+ self .units2 = units2
219+
193220 def yield_units (self ):
194221 yield self .units1
195222 if self .units2 :
@@ -205,16 +232,19 @@ def __str__(self):
205232 )
206233
207234 def __reduce__ (self ):
208- return self .__class__ , tuple ( getattr ( self , f . name ) for f in fields ( self ) )
235+ return self .__class__ , ( self . units1 , self . units2 )
209236
210237
211- @dataclass (frozen = False )
212238class LogarithmicUnitCalculusError (PintTypeError ):
213239 """Raised on inappropriate operations with logarithmic units."""
214240
215241 units1 : ty .Any
216242 units2 : ty .Optional [ty .Any ] = None
217243
244+ def __init__ (self , units1 : ty .Any , units2 : ty .Optional [ty .Any ] = None ) -> None :
245+ self .units1 = units1
246+ self .units2 = units2
247+
218248 def yield_units (self ):
219249 yield self .units1
220250 if self .units2 :
@@ -230,26 +260,28 @@ def __str__(self):
230260 )
231261
232262 def __reduce__ (self ):
233- return self .__class__ , tuple ( getattr ( self , f . name ) for f in fields ( self ) )
263+ return self .__class__ , ( self . units1 , self . units2 )
234264
235265
236- @dataclass (frozen = False )
237266class UnitStrippedWarning (UserWarning , PintError ):
238267 msg : str
239268
269+ def __init__ (self , msg : str ):
270+ self .msg = msg
271+
240272 def __reduce__ (self ):
241- return self .__class__ , tuple ( getattr ( self , f . name ) for f in fields ( self ) )
273+ return self .__class__ , ( self . msg , )
242274
243275
244- @dataclass (frozen = False )
245276class UnexpectedScaleInContainer (Exception ):
246- def __reduce__ (self ):
247- return self .__class__ , tuple (getattr (self , f .name ) for f in fields (self ))
277+ pass
248278
249279
250- @dataclass (frozen = False )
251280class UndefinedBehavior (UserWarning , PintError ):
252281 msg : str
253282
283+ def __init__ (self , msg : str ):
284+ self .msg = msg
285+
254286 def __reduce__ (self ):
255- return self .__class__ , tuple ( getattr ( self , f . name ) for f in fields ( self ) )
287+ return self .__class__ , ( self . msg , )
0 commit comments