Return the tolerance for the comparison. This could be either an absolute tolerance or a relative tolerance, depending on what the user specified or which would be larger.
(self)
| 462 | |
| 463 | @property |
| 464 | def tolerance(self): |
| 465 | """Return the tolerance for the comparison. |
| 466 | |
| 467 | This could be either an absolute tolerance or a relative tolerance, |
| 468 | depending on what the user specified or which would be larger. |
| 469 | """ |
| 470 | |
| 471 | def set_default(x, default): |
| 472 | return x if x is not None else default |
| 473 | |
| 474 | # Figure out what the absolute tolerance should be. ``self.abs`` is |
| 475 | # either None or a value specified by the user. |
| 476 | absolute_tolerance = set_default(self.abs, self.DEFAULT_ABSOLUTE_TOLERANCE) |
| 477 | |
| 478 | if absolute_tolerance < 0: |
| 479 | raise ValueError( |
| 480 | f"absolute tolerance can't be negative: {absolute_tolerance}" |
| 481 | ) |
| 482 | if math.isnan(absolute_tolerance): |
| 483 | raise ValueError("absolute tolerance can't be NaN.") |
| 484 | |
| 485 | # If the user specified an absolute tolerance but not a relative one, |
| 486 | # just return the absolute tolerance. |
| 487 | if self.rel is None: |
| 488 | if self.abs is not None: |
| 489 | return absolute_tolerance |
| 490 | |
| 491 | # Figure out what the relative tolerance should be. ``self.rel`` is |
| 492 | # either None or a value specified by the user. This is done after |
| 493 | # we've made sure the user didn't ask for an absolute tolerance only, |
| 494 | # because we don't want to raise errors about the relative tolerance if |
| 495 | # we aren't even going to use it. |
| 496 | relative_tolerance = set_default( |
| 497 | self.rel, self.DEFAULT_RELATIVE_TOLERANCE |
| 498 | ) * abs(self.expected) |
| 499 | |
| 500 | if relative_tolerance < 0: |
| 501 | raise ValueError( |
| 502 | f"relative tolerance can't be negative: {relative_tolerance}" |
| 503 | ) |
| 504 | if math.isnan(relative_tolerance): |
| 505 | raise ValueError("relative tolerance can't be NaN.") |
| 506 | |
| 507 | # Return the larger of the relative and absolute tolerances. |
| 508 | return max(relative_tolerance, absolute_tolerance) |
| 509 | |
| 510 | |
| 511 | class ApproxDecimal(ApproxScalar): |
nothing calls this directly
no outgoing calls
no test coverage detected