|
1 | 1 | import math
|
2 | 2 | import sys
|
3 |
| - |
4 | 3 | import py
|
5 | 4 |
|
6 | 5 | from _pytest.compat import isclass, izip
|
@@ -32,6 +31,12 @@ def __eq__(self, actual):
|
32 | 31 |
|
33 | 32 | __hash__ = None
|
34 | 33 |
|
| 34 | + def __gt__(self, actual): |
| 35 | + raise NotImplementedError |
| 36 | + |
| 37 | + def __lt__(self, actual): |
| 38 | + raise NotImplementedError |
| 39 | + |
35 | 40 | def __ne__(self, actual):
|
36 | 41 | return not (actual == self)
|
37 | 42 |
|
@@ -60,6 +65,12 @@ def __repr__(self):
|
60 | 65 | return "approx({0!r})".format(list(
|
61 | 66 | self._approx_scalar(x) for x in self.expected))
|
62 | 67 |
|
| 68 | + def __gt__(self, actual): |
| 69 | + raise NotImplementedError |
| 70 | + |
| 71 | + def __lt__(self, actual): |
| 72 | + raise NotImplementedError |
| 73 | + |
63 | 74 | def __eq__(self, actual):
|
64 | 75 | import numpy as np
|
65 | 76 |
|
@@ -358,6 +369,22 @@ def approx(expected, rel=None, abs=None, nan_ok=False):
|
358 | 369 | is asymmetric and you can think of ``b`` as the reference value. In the
|
359 | 370 | special case that you explicitly specify an absolute tolerance but not a
|
360 | 371 | relative tolerance, only the absolute tolerance is considered.
|
| 372 | + |
| 373 | + .. warning:: |
| 374 | +
|
| 375 | + In order to avoid inconsistent behavior, a ``NotImplementedError`` is |
| 376 | + raised for ``__lt__`` and ``__gt__`` comparisons. The example below |
| 377 | + illustrates the problem:: |
| 378 | +
|
| 379 | + assert approx(0.1) > 0.1 + 1e-10 # calls approx(0.1).__gt__(0.1 + 1e-10) |
| 380 | + assert 0.1 + 1e-10 > approx(0.1) # calls approx(0.1).__lt__(0.1 + 1e-10) |
| 381 | +
|
| 382 | + In the second example one expects ``approx(0.1).__le__(0.1 + 1e-10)`` |
| 383 | + to be called. But instead, ``approx(0.1).__lt__(0.1 + 1e-10)`` is used to |
| 384 | + comparison. This is because the call hierarchy of rich comparisons |
| 385 | + follows a fixed behavior. `More information...`__ |
| 386 | +
|
| 387 | + __ https://docs.python.org/3/reference/datamodel.html#object.__ge__ |
361 | 388 | """
|
362 | 389 |
|
363 | 390 | from collections import Mapping, Sequence
|
|
0 commit comments