Return whether the given value is equal to the expected value within the pre-specified tolerance.
(self, actual)
| 435 | return f"{self.expected} ± {vetted_tolerance}" |
| 436 | |
| 437 | def __eq__(self, actual) -> bool: |
| 438 | """Return whether the given value is equal to the expected value |
| 439 | within the pre-specified tolerance.""" |
| 440 | |
| 441 | def is_bool(val: Any) -> bool: |
| 442 | # Check if `val` is a native bool or numpy bool. |
| 443 | if isinstance(val, bool): |
| 444 | return True |
| 445 | if np := sys.modules.get("numpy"): |
| 446 | return isinstance(val, np.bool_) |
| 447 | return False |
| 448 | |
| 449 | asarray = _as_numpy_array(actual) |
| 450 | if asarray is not None: |
| 451 | # Call ``__eq__()`` manually to prevent infinite-recursion with |
| 452 | # numpy<1.13. See #3748. |
| 453 | return all(self.__eq__(a) for a in asarray.flat) |
| 454 | |
| 455 | # Short-circuit exact equality, except for bool and np.bool_ |
| 456 | if is_bool(self.expected) and not is_bool(actual): |
| 457 | return False |
| 458 | elif actual == self.expected: |
| 459 | return True |
| 460 | |
| 461 | # If either type is non-numeric, fall back to strict equality. |
| 462 | # NB: we need Complex, rather than just Number, to ensure that __abs__, |
| 463 | # __sub__, and __float__ are defined. Also, consider bool to be |
| 464 | # non-numeric, even though it has the required arithmetic. |
| 465 | if is_bool(self.expected) or not ( |
| 466 | isinstance(self.expected, Complex | Decimal) |
| 467 | and isinstance(actual, Complex | Decimal) |
| 468 | ): |
| 469 | return False |
| 470 | |
| 471 | # Allow the user to control whether NaNs are considered equal to each |
| 472 | # other or not. The abs() calls are for compatibility with complex |
| 473 | # numbers. |
| 474 | if math.isnan(abs(self.expected)): |
| 475 | return self.nan_ok and math.isnan(abs(actual)) |
| 476 | |
| 477 | # Infinity shouldn't be approximately equal to anything but itself, but |
| 478 | # if there's a relative tolerance, it will be infinite and infinity |
| 479 | # will seem approximately equal to everything. The equal-to-itself |
| 480 | # case would have been short circuited above, so here we can just |
| 481 | # return false if the expected value is infinite. The abs() call is |
| 482 | # for compatibility with complex numbers. |
| 483 | if math.isinf(abs(self.expected)): |
| 484 | return False |
| 485 | |
| 486 | # Return true if the two numbers are within the tolerance. |
| 487 | result: bool = abs(self.expected - actual) <= self.tolerance |
| 488 | return result |
| 489 | |
| 490 | __hash__ = None |
| 491 |
nothing calls this directly
no test coverage detected