Return whether the given value is equal to the expected value within the pre-specified tolerance.
(self, actual)
| 417 | return f"{self.expected} ± {vetted_tolerance}" |
| 418 | |
| 419 | def __eq__(self, actual) -> bool: |
| 420 | """Return whether the given value is equal to the expected value |
| 421 | within the pre-specified tolerance.""" |
| 422 | asarray = _as_numpy_array(actual) |
| 423 | if asarray is not None: |
| 424 | # Call ``__eq__()`` manually to prevent infinite-recursion with |
| 425 | # numpy<1.13. See #3748. |
| 426 | return all(self.__eq__(a) for a in asarray.flat) |
| 427 | |
| 428 | # Short-circuit exact equality. |
| 429 | if actual == self.expected: |
| 430 | return True |
| 431 | |
| 432 | # If either type is non-numeric, fall back to strict equality. |
| 433 | # NB: we need Complex, rather than just Number, to ensure that __abs__, |
| 434 | # __sub__, and __float__ are defined. |
| 435 | if not ( |
| 436 | isinstance(self.expected, (Complex, Decimal)) |
| 437 | and isinstance(actual, (Complex, Decimal)) |
| 438 | ): |
| 439 | return False |
| 440 | |
| 441 | # Allow the user to control whether NaNs are considered equal to each |
| 442 | # other or not. The abs() calls are for compatibility with complex |
| 443 | # numbers. |
| 444 | if math.isnan(abs(self.expected)): # type: ignore[arg-type] |
| 445 | return self.nan_ok and math.isnan(abs(actual)) # type: ignore[arg-type] |
| 446 | |
| 447 | # Infinity shouldn't be approximately equal to anything but itself, but |
| 448 | # if there's a relative tolerance, it will be infinite and infinity |
| 449 | # will seem approximately equal to everything. The equal-to-itself |
| 450 | # case would have been short circuited above, so here we can just |
| 451 | # return false if the expected value is infinite. The abs() call is |
| 452 | # for compatibility with complex numbers. |
| 453 | if math.isinf(abs(self.expected)): # type: ignore[arg-type] |
| 454 | return False |
| 455 | |
| 456 | # Return true if the two numbers are within the tolerance. |
| 457 | result: bool = abs(self.expected - actual) <= self.tolerance |
| 458 | return result |
| 459 | |
| 460 | # Ignore type because of https://github.com/python/mypy/issues/4266. |
| 461 | __hash__ = None # type: ignore |
nothing calls this directly
no test coverage detected