@@ -262,19 +262,22 @@ def _repr_compare(self, other_side: Mapping[object, float]) -> list[str]:
262262 ):
263263 if approx_value != other_value :
264264 if approx_value .expected is not None and other_value is not None :
265- max_abs_diff = max (
266- max_abs_diff , abs (approx_value .expected - other_value )
267- )
268- if approx_value .expected == 0.0 :
269- max_rel_diff = math .inf
270- else :
271- max_rel_diff = max (
272- max_rel_diff ,
273- abs (
274- (approx_value .expected - other_value )
275- / approx_value .expected
276- ),
265+ try :
266+ max_abs_diff = max (
267+ max_abs_diff , abs (approx_value .expected - other_value )
277268 )
269+ if approx_value .expected == 0.0 :
270+ max_rel_diff = math .inf
271+ else :
272+ max_rel_diff = max (
273+ max_rel_diff ,
274+ abs (
275+ (approx_value .expected - other_value )
276+ / approx_value .expected
277+ ),
278+ )
279+ except ZeroDivisionError :
280+ pass
278281 different_ids .append (approx_key )
279282
280283 message_data = [
@@ -398,8 +401,10 @@ def __repr__(self) -> str:
398401 # Don't show a tolerance for values that aren't compared using
399402 # tolerances, i.e. non-numerics and infinities. Need to call abs to
400403 # handle complex numbers, e.g. (inf + 1j).
401- if (not isinstance (self .expected , (Complex , Decimal ))) or math .isinf (
402- abs (self .expected )
404+ if (
405+ isinstance (self .expected , bool )
406+ or (not isinstance (self .expected , (Complex , Decimal )))
407+ or math .isinf (abs (self .expected ) or isinstance (self .expected , bool ))
403408 ):
404409 return str (self .expected )
405410
@@ -427,14 +432,17 @@ def __eq__(self, actual) -> bool:
427432 # numpy<1.13. See #3748.
428433 return all (self .__eq__ (a ) for a in asarray .flat )
429434
430- # Short-circuit exact equality.
431- if actual == self .expected :
435+ # Short-circuit exact equality, except for bool
436+ if isinstance (self .expected , bool ) and not isinstance (actual , bool ):
437+ return False
438+ elif actual == self .expected :
432439 return True
433440
434441 # If either type is non-numeric, fall back to strict equality.
435442 # NB: we need Complex, rather than just Number, to ensure that __abs__,
436- # __sub__, and __float__ are defined.
437- if not (
443+ # __sub__, and __float__ are defined. Also, consider bool to be
444+ # nonnumeric, even though it has the required arithmetic.
445+ if isinstance (self .expected , bool ) or not (
438446 isinstance (self .expected , (Complex , Decimal ))
439447 and isinstance (actual , (Complex , Decimal ))
440448 ):
0 commit comments