7
7
from _pytest .runner import fail
8
8
import _pytest ._code
9
9
10
+
11
+ def _cmp_raises_type_error (self , other ):
12
+ """__cmp__ implementation which raises TypeError. Used
13
+ by Approx base classes to implement only == and != and raise a
14
+ TypeError for other comparisons.
15
+
16
+ Needed in Python 2 only, Python 3 all it takes is not implementing the
17
+ other operators at all.
18
+ """
19
+ __tracebackhide__ = True
20
+ raise TypeError ('Comparison operators other than == and != not supported by approx objects' )
21
+
22
+
10
23
# builtin pytest.approx helper
11
24
12
25
@@ -32,15 +45,12 @@ def __eq__(self, actual):
32
45
33
46
__hash__ = None
34
47
35
- def __gt__ (self , actual ):
36
- raise NotImplementedError
37
-
38
- def __lt__ (self , actual ):
39
- raise NotImplementedError
40
-
41
48
def __ne__ (self , actual ):
42
49
return not (actual == self )
43
50
51
+ if sys .version_info .major == 2 :
52
+ __cmp__ = _cmp_raises_type_error
53
+
44
54
def _approx_scalar (self , x ):
45
55
return ApproxScalar (x , rel = self .rel , abs = self .abs , nan_ok = self .nan_ok )
46
56
@@ -66,11 +76,8 @@ def __repr__(self):
66
76
return "approx({0!r})" .format (list (
67
77
self ._approx_scalar (x ) for x in self .expected ))
68
78
69
- def __gt__ (self , actual ):
70
- raise NotImplementedError
71
-
72
- def __lt__ (self , actual ):
73
- raise NotImplementedError
79
+ if sys .version_info .major == 2 :
80
+ __cmp__ = _cmp_raises_type_error
74
81
75
82
def __eq__ (self , actual ):
76
83
import numpy as np
@@ -370,12 +377,14 @@ def approx(expected, rel=None, abs=None, nan_ok=False):
370
377
is asymmetric and you can think of ``b`` as the reference value. In the
371
378
special case that you explicitly specify an absolute tolerance but not a
372
379
relative tolerance, only the absolute tolerance is considered.
373
-
380
+
374
381
.. warning::
375
382
376
- In order to avoid inconsistent behavior, a ``NotImplementedError`` is
377
- raised for ``__lt__`` and ``__gt__`` comparisons. The example below
378
- illustrates the problem::
383
+ .. versionchanged:: 3.2
384
+
385
+ In order to avoid inconsistent behavior, ``TypeError`` is
386
+ raised for ``>``, ``>=``, ``<`` and ``<=`` comparisons.
387
+ The example below illustrates the problem::
379
388
380
389
assert approx(0.1) > 0.1 + 1e-10 # calls approx(0.1).__gt__(0.1 + 1e-10)
381
390
assert 0.1 + 1e-10 > approx(0.1) # calls approx(0.1).__lt__(0.1 + 1e-10)
0 commit comments