@@ -259,19 +259,22 @@ def _repr_compare(self, other_side: Mapping[object, float]) -> list[str]:
259259 ):
260260 if approx_value != other_value :
261261 if approx_value .expected is not None and other_value is not None :
262- max_abs_diff = max (
263- max_abs_diff , abs (approx_value .expected - other_value )
264- )
265- if approx_value .expected == 0.0 :
266- max_rel_diff = math .inf
267- else :
268- max_rel_diff = max (
269- max_rel_diff ,
270- abs (
271- (approx_value .expected - other_value )
272- / approx_value .expected
273- ),
262+ try :
263+ max_abs_diff = max (
264+ max_abs_diff , abs (approx_value .expected - other_value )
274265 )
266+ if approx_value .expected == 0.0 :
267+ max_rel_diff = math .inf
268+ else :
269+ max_rel_diff = max (
270+ max_rel_diff ,
271+ abs (
272+ (approx_value .expected - other_value )
273+ / approx_value .expected
274+ ),
275+ )
276+ except ZeroDivisionError :
277+ pass
275278 different_ids .append (approx_key )
276279
277280 message_data = [
@@ -395,8 +398,10 @@ def __repr__(self) -> str:
395398 # Don't show a tolerance for values that aren't compared using
396399 # tolerances, i.e. non-numerics and infinities. Need to call abs to
397400 # handle complex numbers, e.g. (inf + 1j).
398- if (not isinstance (self .expected , (Complex , Decimal ))) or math .isinf (
399- abs (self .expected )
401+ if (
402+ isinstance (self .expected , bool )
403+ or (not isinstance (self .expected , (Complex , Decimal )))
404+ or math .isinf (abs (self .expected ) or isinstance (self .expected , bool ))
400405 ):
401406 return str (self .expected )
402407
@@ -428,14 +433,17 @@ def __eq__(self, actual) -> bool:
428433 # numpy<1.13. See #3748.
429434 return all (self .__eq__ (a ) for a in asarray .flat )
430435
431- # Short-circuit exact equality.
432- if actual == self .expected :
436+ # Short-circuit exact equality, except for bool
437+ if isinstance (self .expected , bool ) and not isinstance (actual , bool ):
438+ return False
439+ elif actual == self .expected :
433440 return True
434441
435442 # If either type is non-numeric, fall back to strict equality.
436443 # NB: we need Complex, rather than just Number, to ensure that __abs__,
437- # __sub__, and __float__ are defined.
438- if not (
444+ # __sub__, and __float__ are defined. Also, consider bool to be
445+ # nonnumeric, even though it has the required arithmetic.
446+ if isinstance (self .expected , bool ) or not (
439447 isinstance (self .expected , (Complex , Decimal ))
440448 and isinstance (actual , (Complex , Decimal ))
441449 ):
0 commit comments