2222Aggregate = typing .Literal ["average" , "sum" , "at least one" , "all" ]
2323Rule = typing .Literal ["is above" , "is below" , "is inside range" , "is outside range" ]
2424
25+ try :
26+ from typing import override
27+ except ImportError :
28+ from typing_extensions import override # noqa: UP035
29+
2530
2631class MetricsThresholdAlert (AlertBase ):
2732 """
@@ -134,6 +139,12 @@ def new(
134139
135140 return _alert
136141
142+ @override
143+ def _compare_objects (self , other : "AlertBase" ) -> bool :
144+ if not isinstance (other , MetricsThresholdAlert ):
145+ return False
146+ return super ()._compare_objects (other ) and self .alert == other .alert
147+
137148
138149class MetricsRangeAlert (AlertBase ):
139150 """
@@ -169,9 +180,12 @@ def __init__(self, identifier: str | None = None, **kwargs) -> None:
169180 "range_high" ,
170181 ]
171182
172- def compare (self , other : "MetricsRangeAlert" ) -> bool :
183+ @override
184+ def _compare_objects (self , other : "AlertBase" ) -> bool :
173185 """Compare two MetricRangeAlerts"""
174- return self .alert .compare (other ) if super ().compare (other ) else False
186+ if not isinstance (other , MetricsRangeAlert ):
187+ return False
188+ return super ()._compare_objects (other ) and self .alert == other .alert
175189
176190 @classmethod
177191 @pydantic .validate_call
@@ -258,7 +272,7 @@ def __init__(self, alert: MetricsRangeAlert) -> None:
258272 """Initialise definition with target alert"""
259273 self ._sv_obj = alert
260274
261- def compare (self , other : "MetricsAlertDefinition" ) -> bool :
275+ def __eq__ (self , other : "MetricsAlertDefinition" ) -> bool :
262276 """Compare a MetricsAlertDefinition with another"""
263277 return all (
264278 [
@@ -272,7 +286,7 @@ def compare(self, other: "MetricsAlertDefinition") -> bool:
272286 @property
273287 def aggregation (self ) -> Aggregate :
274288 """Retrieve the aggregation strategy for this alert"""
275- if not (_aggregation := self ._sv_obj .get_alert ().get ("aggregation" )):
289+ if (_aggregation := self ._sv_obj .get_alert ().get ("aggregation" )) is None :
276290 raise RuntimeError (
277291 "Expected key 'aggregation' in alert definition retrieval"
278292 )
@@ -281,14 +295,14 @@ def aggregation(self) -> Aggregate:
281295 @property
282296 def rule (self ) -> Rule :
283297 """Retrieve the rule for this alert"""
284- if not (_rule := self ._sv_obj .get_alert ().get ("rule" )):
298+ if (_rule := self ._sv_obj .get_alert ().get ("rule" )) is None :
285299 raise RuntimeError ("Expected key 'rule' in alert definition retrieval" )
286300 return _rule
287301
288302 @property
289303 def window (self ) -> int :
290304 """Retrieve the aggregation window for this alert"""
291- if not (_window := self ._sv_obj .get_alert ().get ("window" )):
305+ if (_window := self ._sv_obj .get_alert ().get ("window" )) is None :
292306 raise RuntimeError ("Expected key 'window' in alert definition retrieval" )
293307 return _window
294308
@@ -315,32 +329,31 @@ def frequency(self, frequency: int) -> None:
315329class MetricThresholdAlertDefinition (MetricsAlertDefinition ):
316330 """Alert definition for metric threshold alerts"""
317331
318- def compare (self , other : "MetricThresholdAlertDefinition" ) -> bool :
332+ def __eq__ (self , other : "MetricThresholdAlertDefinition" ) -> bool :
319333 """Compare this MetricThresholdAlertDefinition with another"""
320- if not isinstance ( other , MetricThresholdAlertDefinition ):
334+ if not super (). __eq__ ( other ):
321335 return False
322336
323- return all ([ super (). compare ( other ), self .threshold == other .threshold ])
337+ return self .threshold == other .threshold
324338
325339 @property
326340 def threshold (self ) -> float :
327341 """Retrieve the threshold value for this alert"""
328- if not (threshold_l := self ._sv_obj .get_alert ().get ("threshold" )):
342+ if (threshold_l := self ._sv_obj .get_alert ().get ("threshold" )) is None :
329343 raise RuntimeError ("Expected key 'threshold' in alert definition retrieval" )
330344 return threshold_l
331345
332346
333347class MetricRangeAlertDefinition (MetricsAlertDefinition ):
334348 """Alert definition for metric range alerts"""
335349
336- def compare (self , other : "MetricRangeAlertDefinition" ) -> bool :
350+ def __eq__ (self , other : "MetricRangeAlertDefinition" ) -> bool :
337351 """Compare a MetricRangeAlertDefinition with another"""
338- if not isinstance ( other , MetricRangeAlertDefinition ):
352+ if not super (). __eq__ ( other ):
339353 return False
340354
341355 return all (
342356 [
343- super ().compare (other ),
344357 self .range_high == other .range_high ,
345358 self .range_low == other .range_low ,
346359 ]
@@ -349,14 +362,14 @@ def compare(self, other: "MetricRangeAlertDefinition") -> bool:
349362 @property
350363 def range_low (self ) -> float :
351364 """Retrieve the lower limit for metric range"""
352- if not (range_l := self ._sv_obj .get_alert ().get ("range_low" )):
365+ if (range_l := self ._sv_obj .get_alert ().get ("range_low" )) is None :
353366 raise RuntimeError ("Expected key 'range_low' in alert definition retrieval" )
354367 return range_l
355368
356369 @property
357370 def range_high (self ) -> float :
358371 """Retrieve upper limit for metric range"""
359- if not (range_u := self ._sv_obj .get_alert ().get ("range_high" )):
372+ if (range_u := self ._sv_obj .get_alert ().get ("range_high" )) is None :
360373 raise RuntimeError (
361374 "Expected key 'range_high' in alert definition retrieval"
362375 )
0 commit comments