@@ -262,19 +262,22 @@ def _repr_compare(self, other_side: Mapping[object, float]) -> list[str]:
262
262
):
263
263
if approx_value != other_value :
264
264
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 )
277
268
)
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
278
281
different_ids .append (approx_key )
279
282
280
283
message_data = [
@@ -398,8 +401,10 @@ def __repr__(self) -> str:
398
401
# Don't show a tolerance for values that aren't compared using
399
402
# tolerances, i.e. non-numerics and infinities. Need to call abs to
400
403
# 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 ))
403
408
):
404
409
return str (self .expected )
405
410
@@ -427,14 +432,17 @@ def __eq__(self, actual) -> bool:
427
432
# numpy<1.13. See #3748.
428
433
return all (self .__eq__ (a ) for a in asarray .flat )
429
434
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 :
432
439
return True
433
440
434
441
# If either type is non-numeric, fall back to strict equality.
435
442
# 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 (
438
446
isinstance (self .expected , (Complex , Decimal ))
439
447
and isinstance (actual , (Complex , Decimal ))
440
448
):
0 commit comments