@@ -259,19 +259,22 @@ def _repr_compare(self, other_side: Mapping[object, float]) -> list[str]:
259
259
):
260
260
if approx_value != other_value :
261
261
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 )
274
265
)
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
275
278
different_ids .append (approx_key )
276
279
277
280
message_data = [
@@ -395,8 +398,10 @@ def __repr__(self) -> str:
395
398
# Don't show a tolerance for values that aren't compared using
396
399
# tolerances, i.e. non-numerics and infinities. Need to call abs to
397
400
# 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 ))
400
405
):
401
406
return str (self .expected )
402
407
@@ -428,14 +433,17 @@ def __eq__(self, actual) -> bool:
428
433
# numpy<1.13. See #3748.
429
434
return all (self .__eq__ (a ) for a in asarray .flat )
430
435
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 :
433
440
return True
434
441
435
442
# If either type is non-numeric, fall back to strict equality.
436
443
# 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 (
439
447
isinstance (self .expected , (Complex , Decimal ))
440
448
and isinstance (actual , (Complex , Decimal ))
441
449
):
0 commit comments